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/Equality below:

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

相等(==)

Baseline Widely available

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

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

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

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

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

相等运算符(== 和 !=)提供非严格相等语义。这可以大致总结如下:

  1. 如果操作数具有相同的类型,则按如下方式进行比较:
  2. 如果其中一个操作数为 null 或 undefined,另一个操作数也必须为 null 或 undefined 以返回 true。否则返回 false。
  3. 如果其中一个操作数是对象,另一个是原始值,则将对象转换为原始值。
  4. 在这一步,两个操作数都被转换为原始值(字符串、数字、布尔值、符号和大整型中的一个)。剩余的转换将分情况完成。

宽松相等是对称的:A == B 对于 A 和 B 的任何值总是具有与 B == A 相同的语义(应用转换的顺序除外)。

该运算符与严格相等(===)运算符之间最显著的区别是,严格相等运算符不尝试类型转换。相反,严格相等运算符总是认为不同类型的操作数是不同的。严格相等运算符本质上只执行第 1 步,然后对所有其他情况返回 false。

上面的算法有一个“故意违反”:如果其中一个操作数是 document.all,则它被视为 undefined。这意味着 document.all == null 是 true,但 document.all === undefined && document.all === null 是 false。

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

console.log(object1 == object2); // false
console.log(object1 == object1); // 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
比较日期和字符串
const d = new Date("December 17, 1995 03:24:00");
const s = d.toString(); // 例如:“Sun Dec 17 1995 03:24:00 GMT+0800 (中国标准时间)”
console.log(d == s); // true
比较数组和字符串
const a = [1, 2, 3];
const b = "1,2,3";
a == b; // true,`a` 转换为字符串

const c = [true, 0.5, "hey"];
const d = c.toString(); // "true,0.5,hey"
c == d; // true
规范 浏览器兼容性 参见

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