javascript中==和===操作符的比较

本文详细解释了JavaScript中值类型和引用类型的比较规则。对于数值、字符串和布尔值,使用`==`和`===`进行值的比较;而对象、数组和函数则通过引用进行比较。文章还阐述了不同类型间如何进行转换以确定是否相等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

摘引至"JavaScript: The Definitive Guide, 5th Edition " chapter 5 section 4

 

In JavaScript, numbers, strings, and boolean values are compared by value . In this case, two separate values are involved, and the == and === operators check that these two values are identical.

On the other hand, objects, arrays, and functions are compared by reference . This means that two variables are equal only if they refer to the same object.

 

The following rules determine whether two values are identical according to the === operator:

  • If the two values have different types, they are not identical.

  • If both values are numbers and have the same value, they are identical, unless either or both values are NaN , in which case they are not identical. The NaN value is never identical to any other value, including itself! To check whether a value is NaN , use the global isNaN( ) function.

  • If both values are strings and contain exactly the same characters in the same positions, they are identical. If the strings differ in length or content, they are not identical. Note that in some cases, the Unicode standard allows more than one way to encode the same string. For efficiency, however, JavaScript's string comparison compares strictly on a character-by-character basis, and it assumes that all strings have been converted to a "normalized form" before they are compared. 

  • If both values are the boolean value true or both are the boolean value false , they are identical.

  • If both values refer to the same object, array, or function, they are identical. If they refer to different objects (or arrays or functions) they are not identical, even if both objects have identical properties or both arrays have identical elements.

  • If both values are null or both values are undefined , they are identical.

The following rules determine whether two values are equal according to the == operator:

  • If the two values have the same type, test them for identity. If the values are identical, they are equal; if they are not identical, they are not equal.

  • If the two values do not have the same type, they may still be equal. Use the following rules and type conversions to check for equality:

    • If one value is null and the other is undefined , they are equal.

    • If one value is a number and the other is a string, convert the string to a number and try the comparison again, using the converted value.

    • If either value is TRue , convert it to 1 and try the comparison again. If either value is false , convert it to 0 and try the comparison again.

    • If one value is an object and the other is a number or string, convert the object to a primitive and try the comparison again. An object is converted to a primitive value by either its toString( ) method or its valueOf( ) method. The built-in classes of core JavaScript attempt valueOf( ) conversion before toString( ) conversion, except for the Date class, which performs toString( ) conversion. Objects that are not part of core JavaScript may convert themselves to primitive values in an implementation-defined way.

    • Any other combinations of values are not equal.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值