Home > Web Front-end > JS Tutorial > Equality and inequality operations in Javascript_javascript skills

Equality and inequality operations in Javascript_javascript skills

WBOY
Release: 2016-05-16 18:28:49
Original
1024 people have browsed it
The rules for conversion are as follows:
If one of the operands is of type Boolean, then it is first converted to a numeric type, false is converted to 0, and true is converted to 1.
If the type of one of the operands is a string and the other is a numeric type, then the string is converted to a number for comparison.
If one of the operands is of type string and the other is of type object, then the toString method of the object will be called and the strings will be compared.
If the type of one of the operands is a numeric type and the other is an object type, then the object is converted to a numeric value and the numeric comparison is performed.

The following specifies some special comparisons:
null and undefined are equal.
null and undefined are not converted to any other type
If the result of either operation is NaN, then equality comparison returns false and inequality comparison returns true. Note that even if both operands are NaN, the return result is still false, that is, NaN is not equal to NaN.
If both operands are objects, then compare the values ​​they refer to. If they refer to the same object, then return true, otherwise, return false.

alert(null == undefined); // true
alert(undefined == null); // true

alert(true == 1); // true
alert(false == 0); // true
alert(true == 2); // false

var obj = {};
alert(10 == obj); / / false

Exactly the same comparison === and not exactly equal!==
Exactly the same comparison is used to compare whether there is equality without conversion, for example:

var a = " 100";
var b = 100;

alert(a == b); // true
alert(a === b); // false

= The = comparison will return true because "100" is first converted to the number 100, and then compared with the number 100, resulting in equality.
=== The comparison will return false because the string "100" is not equal to the number 100 without conversion.

!== is used to compare whether they are equal without conversion.

alert(a != b); // false
alert(a !== b); // true

The first case will return false because after conversion equal. The second case will return true, because without conversion, one is a string and the other is a number, which are not equal.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template