How to understand =, == and === in JavaScript
迷茫
迷茫 2017-06-30 09:58:46
0
6
1014

How to understand =, == and === in JavaScript, and how to clarify the relationship between these three?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(6)
大家讲道理

= : assignment operation

==: Used to determine whether two values ​​are equal, but the determination here is the result of implicit conversion. For example

1 == '1';  // true
1 == true; // true
0 == false; // true

===: It is also used to determine whether two values ​​​​are equal, but there is no implicit conversion process, but a direct judgment, so it is also called absolute equality/congruence.

1 === '1';  // false
1 === true; // false
0 === false; // false

1. For basic types such as string and number, there is a difference between == and ===

  • 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 will be unequal

  • Same type comparison, direct "value" comparison, the result will be the same

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

Perform “pointer address” comparison

3. There is a difference between basic types and advanced types, == and ===

  • For ==, convert the advanced type to the basic type and perform "value" comparison

  • Because the types are different, the result of === is false

我想大声告诉你

'=': means assignment,

var a = 1,
    b = 2;
return a = b;    //返回2,为a赋值b,即a为2

'==': Determine whether the values ​​are the same, not the type

var a = 1,
    b = '1';
return a == b;    //返回true

'===': accurate judgment, not only the value but also the type

var a = 1,
    b = '1';
return a === b;    //返回false

1. For basic types such as string and number, there is a difference between == and ===

  • 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 will be unequal

  • Same type comparison, direct "value" comparison, the result will be the same

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

3. There is a difference between basic types and advanced types, == and ===

  • For ==, convert the advanced type to the basic type and perform "value" comparison

  • Because the types are different, the result of === is false

学习ing

= is the assignment operator

let a=b // 将值b赋给变量a

== and === are comparison operators

a == b 比较a与b值是否相等
a === b 比较a与b是否全等,值和类型都要相同
学霸

== will perform implicit data type conversion, === will not, it just compares whether both sides are really equal
http://www.softwhy.com/articl...

仅有的幸福
  1. "=" is used to assign values, assign values ​​directly.

  2. The "==" operator will convert first and then operate.

  3. "==="Absolutely equal, the values ​​and types on both sides must be equal.

巴扎黑

Use as much as possible === ==It’s easy to have problems

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template