search

Home  >  Q&A  >  body text

How to understand =, == and === in JavaScript

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

迷茫迷茫2815 days ago1053

reply all(6)I'll reply

  • 大家讲道理

    大家讲道理2017-06-30 10:00:46

    = : 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

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-06-30 10:00:46

    '=': 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

    reply
    0
  • 学习ing

    学习ing2017-06-30 10:00:46

    = is the assignment operator

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

    == and === are comparison operators

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

    reply
    0
  • 欧阳克

    欧阳克2017-06-30 10:00:46

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

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-06-30 10:00:46

    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.

    reply
    0
  • 巴扎黑

    巴扎黑2017-06-30 10:00:46

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

    reply
    0
  • Cancelreply