From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../reference/operators/equality/index.html | 128 +++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 files/ja/web/javascript/reference/operators/equality/index.html (limited to 'files/ja/web/javascript/reference/operators/equality/index.html') diff --git a/files/ja/web/javascript/reference/operators/equality/index.html b/files/ja/web/javascript/reference/operators/equality/index.html new file mode 100644 index 0000000000..b1fdee1943 --- /dev/null +++ b/files/ja/web/javascript/reference/operators/equality/index.html @@ -0,0 +1,128 @@ +--- +title: 等価 (==) +slug: Web/JavaScript/Reference/Operators/Equality +tags: + - JavaScript + - Language feature + - Operator + - Reference + - 演算子 + - 言語機能 +translation_of: Web/JavaScript/Reference/Operators/Equality +--- +
{{jsSidebar("Operators")}}
+ +

等価演算子 (==) は、二つのオペランドが等しいことを検査し、論理値で結果を返します 厳密等価演算子とは異なり、オペランドの型が異なる場合には型の変換を試みてから比較を行います。

+ +
{{EmbedInteractiveExample("pages/js/expressions-equality.html")}}
+ + + +

構文

+ +
x == y
+
+ +

解説

+ +

等価演算子 (== および !=) は、抽象等価比較アルゴリズムを使用して二つのオペランドを比較します。これは、およそ次のようにまとめることができます。

+ + + +

この演算子と厳密等価 (===) 演算子の最も顕著な違いは、厳密等価演算子が型変換を試みない点です。厳密等価演算は、オペランドの型が異なる場合は常に異なるものと見なします。

+ +

+ +

型変換がない場合の比較

+ +
1 == 1;              // true
+"hello" == "hello";  // true
+ +

型変換がある場合の比較

+ +
"1" ==  1;            // true
+1 == "1";             // true
+0 == false;           // true
+0 == null;            // false
+0 == undefined;       // false
+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"};
+
+object1 == object2 // false
+object2 == object2 // true
+ +

文字列と String オブジェクトの比較

+ +

new String() を使用して構築された文字列はオブジェクトであることに注意してください。文字列リテラルとの比較を行うと、 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
+ +

Date と文字列の比較

+ +
const d = new Date('December 17, 1995 03: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
+ +

仕様書

+ + + + + + + + + + + + +
仕様書
{{SpecName('ESDraft', '#sec-equality-operators', 'Equality operators')}}
+ +

ブラウザーの互換性

+ + + +

{{Compat("javascript.operators.strict_inequality")}}

+ +

関連情報

+ + -- cgit v1.2.3-54-g00ecf