5

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.

blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • 3
    Take a look at this [JavaScript === vs == : Does it matter which “equal” operator I use?][1] [1]: http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use – jiiri Mar 26 '13 at 19:26
  • dont forget to mark answer as accpeted it you got the info you want.. – Pranay Rana Mar 26 '13 at 19:49

4 Answers4

13

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

What have you tried
  • 11,018
  • 4
  • 31
  • 45
  • 4
    Just adding essentially == tries type conversion and compares it. Where === does strict comparison :) – theshadowmonkey Mar 26 '13 at 19:27
  • It's a little different from what your answer describes. They both check if the types are equal, and when they are, they behave identically, but if they're not, the `==` does the extra work of coercing the operands to matching types, and recursively retries the comparison until the types are the same. When the types finally match, the values can finally be compared. –  Mar 26 '13 at 19:48
  • @amnotiam So, how could I Change my answer to make it more correct for future visitors? – What have you tried Mar 26 '13 at 20:54
  • 1
    Best advice I could give would be to study the [*Abstract Equality Comparison Algorithm*](http://es5.github.com/#x11.9.3) and the [*Strict Equality Comparison Algorithm*](http://es5.github.com/#x11.9.6). You'll notice that they both start with a *Type* check of both operands, and when the types match, the algorithms are the same. But when they don't match, the *Abstract* algorithm has a set of rules for type coercion. The rules are a little complex, but really not too bad, and very helpful to understand. –  Mar 26 '13 at 21:02
  • @amnotiam Thanks, I've looked the first link over and it's actually pretty interesting. I'm going to add it to the answer, it might help other people out too. – What have you tried Mar 26 '13 at 21:18
  • 1
    @squint very concise +1 – Lucian Enache Mar 31 '14 at 12:04
5
10 == '10'  // true  | check value: 10 equal 10
10 == 10    // true  | check value: 10 equal 10

10 === '10' // false | check type: int not equal string
10 === 10   // true  | check type: int equal int, check value: 10 equal 10

check type or not.

mayo
  • 81
  • 6
  • This answer leads the reader to believe the value check happens first on `===`. This isn't the case. It compares type first, this fails so it returns `false` before going further. See [The Strict Equality Comparison Algorithm](http://es5.github.com/#x11.9.6) – Paul S. Mar 26 '13 at 19:50
  • 1
    Thank you very much for bringing it to my attention. – mayo Mar 26 '13 at 20:03
3

=== is used for checking value with type..

var str="300";

//this gt execute
if(str==="300")
{
        alert("this alert get executed");
}


//this not execute
if(str===300)
{
        alert("this alert not get executed");    
}

for == below both code is valid because its not check for type

//this get execute
if(str=="300")
{
        alert("this alert get executed");
}


//this get execute
if(str==300)
{
        alert("this alert get executed");
}
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
1

The firts check is just a logical test, the second check is logical and type test.

== check if the one side is equal to the second one, while the === check if the left side is equal to the second one but from the same type.

if("true" == "true")

check if both are the same string

if("true" === "true")

check if both are the same string and both are string values.

Note also there is the !== operator that it does negative value and type comparison.

KodeFor.Me
  • 13,069
  • 27
  • 98
  • 166