Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Equality (==)

BaselineWidely available

Theequality (==) operator checks whether its two operands are equal,returning a Boolean result.Unlike thestrict equality operator,it attempts to convert and compare operands that are of different types.

Try it

console.log(1 == 1);// Expected output: trueconsole.log("hello" == "hello");// Expected output: trueconsole.log("1" == 1);// Expected output: trueconsole.log(0 == false);// Expected output: true

Syntax

js
x == y

Description

The equality operators (== and!=) provide theIsLooselyEqual semantic. This can be roughly summarized as follows:

  1. If the operands have the same type, they are compared as follows:
    • Object: returntrue only if both operands reference the same object.
    • String: returntrue only if both operands have the same characters in the same order.
    • Number: returntrue only if both operands have the same value.+0 and-0 are treated as the same value. If either operand isNaN, returnfalse; so,NaN is never equal toNaN.
    • Boolean: returntrue only if operands are bothtrue or bothfalse.
    • BigInt: returntrue only if both operands have the same value.
    • Symbol: returntrue only if both operands reference the same symbol.
  2. If one of the operands isnull orundefined, the other must also benull orundefined to returntrue. Otherwise returnfalse.
  3. If one of the operands is an object and the other is a primitive,convert the object to a primitive.
  4. At this step, both operands are converted to primitives (one of String, Number, Boolean, Symbol, and BigInt). The rest of the conversion is done case-by-case.
    • If they are of the same type, compare them using step 1.
    • If one of the operands is a Symbol but the other is not, returnfalse.
    • If one of the operands is a Boolean but the other is not,convert the boolean to a number:true is converted to 1, andfalse is converted to 0. Then compare the two operands loosely again.
    • Number to String:convert the string to a number. Conversion failure results inNaN, which will guarantee the equality to befalse.
    • Number to BigInt: compare by their mathematical value. If the number is ±Infinity orNaN, returnfalse.
    • String to BigInt: convert the string to a BigInt using the same algorithm as theBigInt() constructor. If conversion fails, returnfalse.

Loose equality issymmetric:A == B always has identical semantics toB == A for any values ofA andB (except for the order of applied conversions).

The most notable difference between this operator and thestrict equality (===) operator is that the strict equality operator does not attempt type conversion. Instead, the strict equality operator always considers operands of different types to be different. The strict equality operator essentially carries out only step 1, and then returnsfalse for all other cases.

There's a "willful violation" of the above algorithm: if one of the operands isdocument.all, it is treated as if it'sundefined. This means thatdocument.all == null istrue, butdocument.all === undefined && document.all === null isfalse.

Examples

Comparison with no type conversion

js
1 == 1; // true"hello" == "hello"; // true

Comparison with type conversion

js
"1" == 1; // true1 == "1"; // true0 == false; // true0 == null; // false0 == undefined; // false0 == !!null; // true, look at Logical NOT operator0 == !!undefined; // true, look at Logical NOT operatornull == undefined; // trueconst number1 = new Number(3);const number2 = new Number(3);number1 == 3; // truenumber1 == number2; // false

Comparison of objects

js
const object1 = {  key: "value",};const object2 = {  key: "value",};console.log(object1 == object2); // falseconsole.log(object1 == object1); // true

Comparing strings and String objects

Note that strings constructed usingnew String() are objects. If youcompare one of these with a string literal, theString object will beconverted to a string literal and the contents will be compared. However, if bothoperands areString objects, then they are compared as objects and mustreference the same object for comparison to succeed:

js
const string1 = "hello";const string2 = String("hello");const string3 = new String("hello");const string4 = new String("hello");console.log(string1 == string2); // trueconsole.log(string1 == string3); // trueconsole.log(string2 == string3); // trueconsole.log(string3 == string4); // falseconsole.log(string4 == string4); // true

Comparing Dates and strings

js
const d = new Date("1995-12-17T03:24:00");const s = d.toString(); // for example: "Sun Dec 17 1995 03:24:00 GMT-0800 (Pacific Standard Time)"console.log(d == s); // true

Comparing arrays and strings

js
const a = [1, 2, 3];const b = "1,2,3";a == b; // true, `a` converts to stringconst c = [true, 0.5, "hey"];const d = c.toString(); // "true,0.5,hey"c == d; // true

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-equality-operators

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp