Home > Common Problem > The difference between == and === in js

The difference between == and === in js

百草
Release: 2023-06-14 11:52:25
Original
10493 people have browsed it

The difference between "==" and "===" in js: 1. "==" only judges the value but not the data type, while "===" judges the value and also the data type; 2. When using "==" for comparison, the data type can be automatically converted, but "===" cannot automatically convert the data type; 3. "===" is more rigorous, and it is recommended to use "===";

The difference between == and === in js

The operating system of this tutorial: windows10 system, JavaScript ECMAScript 2021 version, DELL G3 computer.

The difference between = = and === in js

= = and === are both used to determine equality , the difference is: equal degree of depth.

1.= = only judges the numerical value but not the data type. In other words, when using = = for comparison, it can automatically convert the data type for us;

2.=== determines the degree of equality deeper than ==, that is, it determines both the numerical value and the data type, and the data type cannot be automatically converted.

1, = = (the judgment values ​​are equal)

let a = 1; //数字类型
let b = ‘1’; //字符类型console.log(a==b);123
Copy after login

Result: true

2, === (congruent, judgment values ​​and types All equal)

let a = 1; //数字类型
let b = ‘1’;//字符类型
console.log(a===b);123
Copy after login

Result: false

Note: = = will automatically convert the data type, so sometimes some problems will occur, for example:

let a = 1;let b = true;console.log(a==b);//结果是true123
let a = 0;let b = false;console.log(a==b);//结果是true123
let a = null;let b = undefined;console.log(a==b);//结果是true123
Copy after login

Summary: === is more rigorous, it is recommended to use ===

The above is the detailed content of The difference between == and === in js. 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