HTML: Markup language
CSS: Styling language
JavaScript: Scripting language
Web APIs: Programming interfaces
All web technology
Learn web development
Discover our tools
Get to know MDN better
このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。
View in English Always switch to English
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
等価演算子 (==) は、 2 つのオペランドが等しいことを検査し、論理値で結果を返します。 厳密等価演算子とは異なり、オペランドの型が異なる場合には型の変換を試みてから比較を行います。
==
console.log(1 == 1); // 予想される結果: true console.log("hello" == "hello"); // 予想される結果: true console.log("1" == 1); // 予想される結果: true console.log(0 == false); // 予想される結果: true
x == y
等価演算子 (== および !=) は、IsLooselyEqual の意味を提供します。これは、およそ次のようにまとめることができます。
!=
true
+0
-0
NaN
false
null
undefined
BigInt()
緩い等価性は対称的です。 A == B は、A と B がいかなる値であっても(変換の適用順序を除いて)、常に B == A と同一の意味を持ちます。
A == B
A
B
B == A
厳密な等価性演算子(===)との最も顕著な違いは、厳密等価演算子が型変換を一切試みない点です。代わりに、厳密等価演算子はオペランドの型が異なると、常に異なるものと見なします。厳密等価演算子は基本的に段階 1 のみを実行し、それ以外の場合はすべて false を返します。
===
上記のアルゴリズムには「意図的な違反」があります。オペランドの一方が document.all の場合、 undefined として扱われます。つまり、 document.all == null は true ですが、 document.all === undefined && document.all === null は false となります。
document.all
document.all == null
document.all === undefined && document.all === null
1 == 1; // true "hello" == "hello"; // true
"1" == 1; // true 1 == "1"; // true 0 == false; // true 0 == null; // false 0 == undefined; // false 0 == !!null; // true (論理 NOT 演算子を参照) 0 == !!undefined; // true (論理 NOT 演算子を参照) null == undefined; // true const number1 = new Number(3); const number2 = new Number(3); number1 == 3; // true number1 == number2; // false
const object1 = { key: "value", }; const object2 = { key: "value", }; console.log(object1 == object2); // false console.log(object1 == object1); // true
new String() を使用して構築された文字列はオブジェクトであることに注意してください。文字列リテラルとの比較を行うと、 String オブジェクトは文字列リテラルに変換され、その中身が比較されます。ただし、両方のオペランドが String オブジェクトであった場合は、オブジェクトとして比較され、同じオブジェクトを参照している場合だけ比較に成功します。
new String()
String
const string1 = "hello"; const string2 = String("hello"); const string3 = new String("hello"); const string4 = new String("hello"); console.log(string1 == string2); // true console.log(string1 == string3); // true console.log(string2 == string3); // true console.log(string3 == string4); // false console.log(string4 == string4); // true
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
const a = [1, 2, 3]; const b = "1,2,3"; a == b; // true。 `a` を文字列に変換 const c = [true, 0.5, "hey"]; const d = c.toString(); // "true,0.5,hey" c == d; // true
Enable JavaScript to view this browser compatibility table.
!==