摘引至"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.
分享到:
相关推荐
主要介绍了JavaScript中的比较操作符>、<、>=、<=介绍,本文先是讲解了操作符的一些使用规则,然后给出了操作实例,需要的朋友可以参考下
第4节 JavaScript操作符.rar第4节 JavaScript操作符.rar第4节 JavaScript操作符.rar第4节 JavaScript操作符.rar第4节 JavaScript操作符.rar第4节 JavaScript操作符.rar第4节 JavaScript操作符.rar第4节 JavaScript...
javascript中的相等操作符(==与===区别).docx
JavaScript常用运算符和操作符总结(教辅)JavaScript常用运算符和操作符总结(教辅)JavaScript常用运算符和操作符总结(教辅)JavaScript常用运算符和操作符总结(教辅)JavaScript常用运算符和操作符总结(教辅)...
JavaScript中,==与===操作符均可用于判断两个值是否相等;不同之处在于,如果进行判断的两个值类型不一致,===操作符会直接返回false,而==操作符则会在类型转换后再进行判断。详细的判断规则如下: ===操作符的...
我们在编程的过程中,经常会遇到判断两个变量是否相等的情况,ECMAscript提供了两种相等操作符”==”和”===”来判断,这两种操作都会返回一个boolean值。一般来说,我们把”==”称之为相等,称”===”为全等。当两...
JavaScript中new操作符的原理示例详解 new的作用是通过构造函数来创建一个实例对象,该实例与原型和构造函数之间的关系如下图所示: 执行 new 操作时会依次经过以下步骤: 1、创建一个空对象 空对象是 Object...
JavaScript常用运算符和操作符总结(教辅).pdf
与==操作符一样,比较操作符(>、<、>=、<=)可以将对象转换成string或者number后再进行比较 — 对于number,比较值的大小;对于string,则比较字符在编码表中出现的次序。与==操作符不一样的是,==会将Date对象...
JavaScript中极易出错的操作符运算总结 目录 算术运算符 非正常情况1: 有特殊值字面量参与的运算非正常情况2: 其他类型的数据参与数学运算。比较运算符 非正常情况1: 特殊值参与比较运算非正常情况2: 其他数据...
本文着重给大家介绍了javascript的操作符,包含一元操作符、位操作符、布尔操作符这3种类型,讲解的十分全面,小伙伴们参考下
JavaScript剩余操作符Rest Operator详解.docx
js运算符操作符
JavaScript操作符及类型转换与流程控制语句.docx
主要介绍了JavaScript中new操作符的解析和实现,帮助大家更好的理解和学习JavaScript,感兴趣的朋友可以了解下
JavaScript展开操作符(Spread operator)详解.docx
对一个值使用typeof...常用的typeof操作符的返回值包括number、string、boolean、undefined 、object和function。如: 代码如下:var n;console.log(typeof n); // “undefined”n = 1;console.log(typeof n); // “n
2.1 准备工作 2.2 语法 2.2.1 语句 2.2.2 注释 2.2.3 变量 2.2.4 数据类型 2.2.5 数组 2.2.6 对象 2.3 操作 2.4 条件语句 2.4.1 比较操作符 2.4.2 逻辑操作符 2.5 循环语句 2.5.1 while循环 2.5.2 for循环 2.6 函数 ...