[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Guide/Equality_comparisons_and_sameness [Back]  [Original]

- JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

JavaScript .

. .

  • (==) , IEEE 754 NaN, -0, +0 ( NaN != NaN -0 == +0).
  • (===) (NaN, -0, +0 ) . false .
  • Object.is() NaN, -0, +0 ( === ).

JavaScript 4 3 .

. . , x y , false .

In this article

===

. . , . . , NaN , +0 -0 .

js
const num = 0;
const obj = new String("0");
const str = "0";

console.log(num === num); // true
console.log(obj === obj); // true
console.log(str === str); // true

console.log(num === obj); // false
console.log(num === str); // false
console.log(obj === str); // false
console.log(null === undefined); // false
console.log(obj === null); // false
console.log(obj === undefined); // false

. " " . . 0 . , +0 -0 . NaN (: ) . NaN . ((x !== x) true x NaN .)

=== Array.prototype.indexOf(), Array.prototype.lastIndexOf(), TypedArray.prototype.indexOf(), TypedArray.prototype.lastIndexOf(), case . , indexOf(NaN) NaN NaN switch case .

js
console.log([NaN].indexOf(NaN)); // -1
switch (NaN) {
  case NaN:
    console.log("Surprise"); //   .
}

==

. , A == B A B B == A ( ). == .

  1. .
    • : true .
    • : true .
    • : true . +0 -0 . NaN false . NaN NaN .
    • : true false true .
    • BigInt: true .
    • Symbol: symbol true .
  2. null undefined true null undefined . false .
  3. .
  4. (, , , Symbol, BigInt ) . .
    • , 1 .
    • Symbol , false .
    • , . true 1 false 0 . .
    • : . NaN false .
    • BigInt: . Infinity NaN false .
    • BigInt: BigInt() BigInt . false .

, ECMAScript undefined null . ( document.all ) undefined . . null == A undefined == A A undefined true . undefined null .

. , .

0, bigint 0n, '0', toString() '0' .

js
const num = 0;
const big = 0n;
const str = "0";
const obj = new String("0");

console.log(num == str); // true
console.log(big == num); // true
console.log(str == big); // true

console.log(num == obj); // true
console.log(big == obj); // true
console.log(str == obj); // true

== .

Object.is()

. ( Liskov .) .

js
//    NEGATIVE_ZERO  Number  .
Object.defineProperty(Number, "NEGATIVE_ZERO", {
  value: -0,
  writable: false,
  configurable: false,
  enumerable: false,
});

function attemptMutation(v) {
  Object.defineProperty(Number, "NEGATIVE_ZERO", { value: v });
}

Object.defineProperty , . v -0 , . , .

Object.is . ID .

, +0 -0 .

JavaScript API , .

js
function sameValueZero(x, y) {
  if (typeof x === "number" && typeof y === "number") {
    // x y (-0 0  )   NaN.
    return x === y || (x !== x && y !== y);
  }
  return x === y;
}

NaN , -0 0 . NaN , . Array.prototype.includes(), TypedArray.prototype.includes(), Map Set .

"" . , . (: 6 == "6"). . .

" " , " " 1 "" . Object.is , "" , "" (, ). Object.is NaN . Object.is(NaN, NaN) false Object.is -0 +0 / . NaN . Object.is .

x y == === Object.is SameValueZero
undefined undefined true true true true
null null true true true true
true true true true true true
false false true true true true
'foo' 'foo' true true true true
0 0 true true true true
+0 -0 true true false true
+0 0 true true true true
-0 0 true true false true
0n -0n true true true true
0 false true false false false
"" false true false false false
"" 0 true false false false
'0' 0 true false false false
'17' 17 true false false false
[1, 2] '1,2' true false false false
new String('foo') 'foo' true false false false
null undefined true false false false
null false false false false false
undefined false false false false false
{ foo: 'bar' } { foo: 'bar' } false false false false
new String('foo') new String('foo') false false false false
0 null false false false false
0 NaN false false false false
'foo' NaN false false false false
NaN NaN false false true true

Object.is() ?

0 Object.is , Object.defineProperty . , Object.is === . NaN true , NaN ( ECMAScript isNaN ) 0 .

-0 +0 .

- ( )

.

js
const stoppingForce = obj.mass * -obj.velocity;

obj.velocity 0( 0 ) -0 stopsingForce .

Math.atan2, Math.ceil, Math.pow, Math.round

-0 -0 . Math.pow -Infinity -0 . .

Math.floor, Math.max, Math.min, Math.sin, Math.sqrt, Math.tan

-0 -0 . Math.min(-0, +0) -0 . , .

~, <<, >>

ToInt32 . 32 0 -0 . , Object.is(~~(-0), -0) Object.is(-0 << 2 >> 2, -0) false .

0 Object.is . -0 +0 .

: Object.is() NaN

Object.is NaN . NaN .

js
const f2b = (x) => new Uint8Array(new Float64Array([x]).buffer);
const b2f = (x) => new Float64Array(x.buffer)[0];
// NaN   .
const n = f2b(NaN);
// NaN        .
n[0] = 1;
const nan2 = b2f(n);
console.log(nan2); // NaN
console.log(Object.is(nan2, NaN)); // true
console.log(f2b(NaN)); // Uint8Array(8) [0, 0, 0, 0, 0, 0, 248, 127]
console.log(f2b(nan2)); // Uint8Array(8) [1, 0, 0, 0, 0, 0, 248, 127]


Web Proxy Viewer  |  New URL  |  Original Page