Home > Web Front-end > JS Tutorial > body text

Examples of judging equality and inequality in Javascript

零下一度
Release: 2017-06-26 11:17:11
Original
2582 people have browsed it

In javascript, you can use == to compare whether two data are equal. If the two data types are different, then they will be compared after conversion. The conversion rules are as follows:

l 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.

l                           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.

l                       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.

l                                                                                                                             behaving with a numeric type and the other be an object type be compared with each other after converting the object to a numeric value.

The following stipulates some special comparisons:

l                       and   undefined  are equal.

l                       null and undefined                                                                                                                                                                                 are not converted to any other type. Note that even if both operands are NaN, the return result is still false, that is, NaN is not equal to NaN.

l 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

Identical comparison == = and not exactly equal !==

Exactly the same comparison is used to compare whether they are equal 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 and the result is equal.

=== 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 above is the detailed content of Examples of judging equality and inequality in Javascript. For more information, please follow other related articles on the PHP Chinese website!

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