Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use === instead of == when comparing a String [duplicate]

When checking for a String value I use '=='. But I have seen instances where '===' is used. For example, instead of

if("true" == "true"){
 alert('true');
}

this is used :

if("true" === "true"){
 alert('true');
}

What is the reasoning behind this ? Both use cases seem to work as expected.

like image 743
blue-sky Avatar asked Nov 30 '22 21:11

blue-sky


1 Answers

The === operator ensures that not only the values are equal, but the two items being compared are of the same type too; Whereas the == operator only checks that the values of the two items are equal

As @amnotiam mentioned in the comments, you may also want to check out the The Abstract Equality Comparison Algorithm

like image 63
What have you tried Avatar answered Dec 09 '22 20:12

What have you tried