1
[4] === 4 // is: false
[4] == 4 // is: true

'0' == 0 // is: true
'0' === 0 // is: false

Can anyone give the exact reason for this?Also what exactly does strict equality operator do or need for comparision?I learned that type and value should be same for strict(===) operator.Is this what strict equality operator checks .If yes,than how equl to operator works?

Maizere Pathak.Nepal
  • 2,383
  • 3
  • 27
  • 41

3 Answers3

4
  • == Compare values
  • === Compare values and type

For example

[4] //turns into "4" when comparing
"4" == 4 //They are the same

"4" === 4 //The values are the same, but not the type

Reference: http://es5.github.io/#x11.9.4

https://i.stack.imgur.com/q13LO.png

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
1

The === operator also compares the type of the object.

So, in [4] === 4

[4] is an array, but 4 is a number, so that evaluates to false.

And in '0' === 0

'0' is a string, but 0 is a number, so that evaluates to false.

painotpi
  • 6,894
  • 1
  • 37
  • 70
0

The === operator compares both type and value, making it a stricter check. The == operator performs a less strict value based checked. It will in some cases consider values of different types "equal" Such examples are 0 vs '' or 0 vs "0"

The == operator will see 0 and '' as equal whereas the === operator will not treat them as equal since they are of different types.

TGH
  • 38,769
  • 12
  • 102
  • 135