Home Web Front-end JS Tutorial Compare the usage differences between == and ===

Compare the usage differences between == and ===

Jul 24, 2017 pm 02:00 PM
the difference



1. For basic types such as string and number, there is a difference between == and ===
1) Comparison between different types , == compares "values ​​converted into the same type" to see if the "values" are equal. === If the types are different, the result is unequal
2) Compare the same type, directly perform the "value" Comparing, the results are the same

2. For advanced types such as Array and Object, there is no difference between == and ===
Compare "pointer address"

3. There is a difference between basic types and advanced types, == and ===
1) For ==, convert the advanced type into a basic type and perform "value" comparison
2) Because the types are different, the result of === is false

Sometimes you will see three equal signs (===) used when judging whether two objects are equal. What is the difference between it and two equal signs (==)? Woolen cloth? To put it simply, when using "==", if the types of both sides are different, the js engine will convert them into the same type and then compare them, while "===" will not perform type conversion, so when the two sides do not belong to the same Type, definitely not equal. For example:

var a = 0, b = '0';

alert((a == b) + '--' + (a === b))
Copy after login

The result seen at this time is "true–false".

=== Judgment rules

  1. If the types are different, [not equal]

  2. If both are numerical 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 two 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].

== Judgment rules:

  1. If the two value types are the same, perform === comparison.

  2. If two value types are different, they may be equal. Perform type conversion and then compare according to the following rules:

  • If one is null and the other is undefined, then [equal].

  • If one is a string and the other is a numerical value, convert the string into a numerical value and then compare.

  • If any value is true, convert it to 1 and compare; if any value is false, convert it to 0 and compare.

  • If one is an object and the other is a numeric 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, let's say (it's more troublesome, I don't quite understand)

  • Any other combination is [not equal].

Special attention needs to be paid to the conversion of true and false, for example:

alert(true == 1);  //ture
alert(true == 2); //false, true会转换成number,也就是1,当然 1 不等于 2
//可以使用 !! 来把一个数据类型转换为boolean型
alert(true == !!2) //true,!2 === false !(!=2) = !false = true
Copy after login

In addition, in js, if a variable is used in a logical operation, then the variable is When there is no initial value or its value is 0, -0, null, "", false, undefined or NaN, its value is false. Otherwise, its value is true.

The above is the detailed content of Compare the usage differences between == and ===. For more information, please follow other related articles on the PHP Chinese website!

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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

What is the function of C language sum? What is the function of C language sum? Apr 03, 2025 pm 02:21 PM

There is no built-in sum function in C language, so it needs to be written by yourself. Sum can be achieved by traversing the array and accumulating elements: Loop version: Sum is calculated using for loop and array length. Pointer version: Use pointers to point to array elements, and efficient summing is achieved through self-increment pointers. Dynamically allocate array version: Dynamically allocate arrays and manage memory yourself, ensuring that allocated memory is freed to prevent memory leaks.

The difference between Ether and Bitcoin What is the difference between Ether and Bitcoin The difference between Ether and Bitcoin What is the difference between Ether and Bitcoin Mar 19, 2025 pm 04:54 PM

The difference between Ethereum and Bitcoin is significant. Technically, Bitcoin uses PoW, and Ether has shifted from PoW to PoS. Trading speed is slow for Bitcoin and Ethereum is fast. In application scenarios, Bitcoin focuses on payment storage, while Ether supports smart contracts and DApps. In terms of issuance, the total amount of Bitcoin is 21 million, and there is no fixed total amount of Ether coins. Each security challenge is available. In terms of market value, Bitcoin ranks first, and the price fluctuations of both are large, but due to different characteristics, the price trend of Ethereum is unique.

The difference between char and wchar_t in C language The difference between char and wchar_t in C language Apr 03, 2025 pm 03:09 PM

In C language, the main difference between char and wchar_t is character encoding: char uses ASCII or extends ASCII, wchar_t uses Unicode; char takes up 1-2 bytes, wchar_t takes up 2-4 bytes; char is suitable for English text, wchar_t is suitable for multilingual text; char is widely supported, wchar_t depends on whether the compiler and operating system support Unicode; char is limited in character range, wchar_t has a larger character range, and special functions are used for arithmetic operations.

What is the difference between bean bread and deepseek What is the difference between bean bread and deepseek Mar 12, 2025 pm 01:24 PM

The core difference between bean bun and DeepSeek is retrieval accuracy and complexity. 1. Doubao is based on keyword matching, simple and direct, with low cost, but low accuracy, and is only suitable for structured data; 2. DeepSeek is based on deep learning, can understand semantics, has high accuracy, but high cost, and is suitable for unstructured data. The final choice depends on the application scenario and resource limitations. If the accuracy requirements are not high, choose bean bags, and if you pursue high precision, choose DeepSeek.

What are the basic requirements for c language functions What are the basic requirements for c language functions Apr 03, 2025 pm 10:06 PM

C language functions are the basis for code modularization and program building. They consist of declarations (function headers) and definitions (function bodies). C language uses values ​​to pass parameters by default, but external variables can also be modified using address pass. Functions can have or have no return value, and the return value type must be consistent with the declaration. Function naming should be clear and easy to understand, using camel or underscore nomenclature. Follow the single responsibility principle and keep the function simplicity to improve maintainability and readability.

How does Apache or Nginx work together with PHP: What is the difference between mod_php5, php-cgi and php-fpm? How does Apache or Nginx work together with PHP: What is the difference between mod_php5, php-cgi and php-fpm? Apr 01, 2025 pm 12:15 PM

The collaborative working mechanism between Apache or Nginx and PHP: Comparison of mod_php5, php-cgi and php-fpm is to use Apache or Nginx to build a web server and use PHP for backend...

What is the difference between `var` and `type` keyword definition structure in Go language? What is the difference between `var` and `type` keyword definition structure in Go language? Apr 02, 2025 pm 12:57 PM

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

See all articles