不等于 (!=)

不等于运算符 (!=) 检查其两个操作数是否不相等,并返回布尔结果。 与严格的不等式运算符不同,它尝试转换和比较不同类型的操作数。

语法

x != y

描述

不等式运算符检查其操作数是否不相等。这是等于运算符的取反,因此以下两行将始终给出相同的结果: 

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
0 != !!null;          // false, look at Logical NOT operator 
0 != !!undefined;     // false, look at Logical NOT operator 
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

规范

Specification
ECMAScript (ECMA-262)
Equality operators

浏览器兼容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
Inequality (a != b)Chrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support 3Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0nodejs Full support 0.1.100

Legend

Full support  
Full support

参见