Technology Sharing

【JavaScript】 The difference between =, ==, ===

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

=: Assignment operator.
==: Equality operator. When the value types on both sides of the equal sign are different, they are first converted to the same type and then compared to see if the values ​​are equal.
===: Strict operator, does not perform type conversion, and returns false if the types are different.
=== is generally used to compare whether they are equal.

//==做类型转换后判断
console.log("1" == true)//true
console.log(1 == true)//true

//===不做类型转换,类型不同则直接false
console.log(1 === true)//false
console.log("1" === "1")//true

//比较object, Array, Function时,比较他们是否指向同一个对象
let arr1 = [1,2,3]
let arr2 = arr1
console.log(arr1 === arr2)

//null和undefined
console.log(null == undefined)//true
console.log(null === undefined)//false
console.log(null === null)//true
console.log(undefined === undefined)//true
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18