不相等(!=)
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
不相等运算符(!=
)检查其两个操作数是否不相等,并返回布尔结果。与严格不相等运算符不同,它会转换并比较不同类型的操作数。
尝试一下
语法
js
x != y
描述
示例
没有类型转换的比较
js
1 != 2; // true
"hello" != "hola"; // true
1 != 1; // false
"hello" != "hello"; // false
有类型转换的比较
js
"1" != 1; // false
1 != "1"; // false
0 != false; // false
0 != null; // true
0 != undefined; // true
0 != !!null; // false,看看逻辑非运算符
0 != !!undefined; // false,看看逻辑非运算符
null != undefined; // false
const number1 = new Number(3);
const number2 = new Number(3);
number1 != 3; // false
number1 != number2; // true
比较对象
js
const object1 = {
key: "value",
};
const object2 = {
key: "value",
};
console.log(object1 != object2); // true
console.log(object1 != object1); // false
规范
Specification |
---|
ECMAScript Language Specification # sec-equality-operators |
浏览器兼容性
BCD tables only load in the browser