Home Web Front-end JS Tutorial Introduction to the difference between two equal signs and three equal signs in javaScript_Basic knowledge

Introduction to the difference between two equal signs and three equal signs in javaScript_Basic knowledge

May 16, 2016 pm 04:43 PM

In a nutshell: == first convert the type and then compare, === first determine the type, if it is not the same type, it will be false directly.
=== means equality, both sides of the comparison must be absolutely the same

alert(0 == ""); // true
alert(0 == false); // true
alert("" == false); // true
Copy after login
alert(0 === ""); // false
alert(0 === false); // false
alert("" === false); // false
Copy after login

Let’s talk about === first. This is relatively simple. The specific comparison rules are as follows:

1. If the types are different, [not equal]
2. If both are numeric values ​​and are the same value, then [equal]; (!Exception) is that if at least one of them is NaN, then [not equal]. (To determine whether a value is NaN, you can only use isNaN() to determine)
3. If both are strings and the characters at each position are the same, then [equal]; otherwise [not equal].
4. If both values ​​are true, or both are false, then [equal].
5. If both values ​​refer to the same object or function, then [equal]; otherwise [not equal].
6. If both values ​​are null, or both are undefined, then [equal].

As for ==, the specific comparison rules are as follows:

1. If the two value types are the same, perform === comparison. The comparison rules are the same as above
2. If two value types are different, they may be equal. Perform type conversion and comparison according to the following rules:
a. If one is null and the other is undefined, then [equal].
b. If one is a string and the other is a numerical value, convert the string into a numerical value and then compare.
c. If any value is true, convert it to 1 and compare; if any value is false, convert it to 0 and compare.
d. If one is an object and the other is a numerical value or string, convert the object into a value of the basic type and then compare. The object is converted to the base type using its toString or valueOf method. JS core built-in classes will try valueOf before toString; the exception is Date, which uses toString conversion. Non-js core objects, Ling Shuo (it’s more troublesome, I don’t quite understand it)
e. Any other combination (array array, etc.) is [not equal].

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

Example Colors JSON File

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

8 Stunning jQuery Page Layout Plugins

10 jQuery Syntax Highlighters 10 jQuery Syntax Highlighters Mar 02, 2025 am 12:32 AM

10 jQuery Syntax Highlighters

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

Build Your Own AJAX Web Applications

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

What is 'this' in JavaScript?

10  JavaScript & jQuery MVC Tutorials 10 JavaScript & jQuery MVC Tutorials Mar 02, 2025 am 01:16 AM

10 JavaScript & jQuery MVC Tutorials

See all articles