A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Inequality below:

不相ç‰ï¼ˆ!=) - JavaScript | MDN

不相等(!=)

Baseline Widely available

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

尝试一下
console.log(1 != 1);
// Expected output: false

console.log("hello" != "hello");
// Expected output: false

console.log("1" != 1);
// Expected output: false

console.log(0 != false);
// Expected output: false
语法 描述

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

有关比较算法的详细信息,请参见相等运算符的页面。

与相等运算符一样,不相等运算符将尝试转换和比较不同类型的操作数:

为避免这种情况,并要求将不同类型视为不同,请使用严格不相等运算符:

示例 没有类型转换的比较
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,看看逻辑非运算符
0 != !!undefined; // false,看看逻辑非运算符
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",
};

console.log(object1 != object2); // true
console.log(object1 != object1); // false
规范 浏览器兼容性 参见

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4