Javascript中===和==的区别

今天正在看Javascript===和==的区别,里面分析的很详细,特分享出来给大家共享:

The strict equality operator  ===evaluates its operands, and then compares the two
values as follows, performing no type conversion:
• If the two values have different types, they are not equal.
• If both values are nullor both values are undefined, they are equal.
• If both values are the boolean value trueor both are the boolean value false, they
are equal.
• If one or both values is NaN, they are not equal. The NaNvalue is never equal to any
other value, including itself! To check whether a value xis NaN, use x !== x. NaNis
the only value of xfor which this expression will be true.
• If both values are numbers and have the same value, they are equal. If one value is
0and the other is -0, they are also equal.
• If both values are strings and contain exactly the same 16-bit values  in the same positions, they are equal. If the strings differ in length or
content, they are not equal. Two strings may have the same meaning and the same
visual appearance, but still be encoded using different sequences of 16-bit values.
JavaScript  performs  no  Unicode  normalization,  and  a  pair  of  strings  like  this
are  not  considered  equal  to  the  === or  to  the  == operators.
• If both values refer to the same object, array, or function, they are equal. If they
refer to different objects they are not equal, even if both objects have identical
properties.
The equality operator ==is like the strict equality operator, but it is less strict. If the
values of the two operands are not the same type, it attempts some type conversions
and tries the comparison again:
• If the two values have the same type, test them for strict equality as described above.
If they are strictly equal, they are equal. If they are not strictly equal, they are not
equal.
• If the two values do not have the same type, the  ==operator may still consider them
equal. Use the following rules and type conversions to check for equality:
—If one value is nulland 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 using the algorithm described 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.

posted @ 2014-04-25 16:42  qingxi_wei  阅读(262)  评论(0编辑  收藏  举报