不等価演算子 (!=
) は、2つのオペランドが等しくないかをチェックし、ブール値の結果を返します。厳密不等価演算子とは異なり、異なる型のオペランドを変換して比較を行おうとします。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
構文
x != y
説明
不等価演算子は、そのオペランドが等しくないかどうかをチェックします。これは等価演算子の否定なので、次の2行は常に同じ結果になります。
x != y
!(x == y)
比較アルゴリズムの詳細については、等価演算子のページを参照して下さい。
等価演算子と同様に、不等価演算子は異なる型のオペランドを変換して比較しようとします。
3 != "3"; // false
これを防止し、異なる型が異なる結果を返すようにするには、代わりに厳密不等価演算子を使用します:
3 !== "3"; // true
例
型変換なしの比較
1 != 2; // true
"hello" != "hola"; // true
1 != 1; // false
"hello" != "hello"; // false
型変換ありの比較
"1" != 1; // false
1 != "1"; // false
0 != false; // false
0 != null; // true
0 != undefined; // true
null != undefined; // false
const number1 = new Number(3);
const number2 = new Number(3);
number1 != 3; // false
number1 != number2; // true
オブジェクトの比較
const object1 = {"key": "value"}
const object2 = {"key": "value"};
object1 != object2 // true
object2 != object2 // false
仕様
ブラウザー実装状況
BCD tables only load in the browser