-1

Possible Duplicate:
JavaScript === vs == : Does it matter which “equal” operator I use?
The 3 different equals

I'm trying to understand what is happening here:

 data.toPage = $('div#someID');

if ( typeof data.toPage === "string" ) {
      // sth
      console.log("hello");
      }

So I'm checking for a string am I not? I'm curious because my console "helloes".

Thanks for some input!

Community
  • 1
  • 1
frequent
  • 27,643
  • 59
  • 181
  • 333

3 Answers3

7

== This is the equal operator and returns a boolean true if both the operands are equal. JavaScript will attempt to convert different data types to the same type in order to make the comparison. Assuming 'a' to be 2 and 'b' to be 4, the following examples will return a value of true:

a == 2
a == "2"
2 == '2' 

=== This is the strict equal operator and only returns a Boolean true if both the operands are equal and of the same type. These next examples return true:

a === 2
b === 4 
Pieter
  • 3,339
  • 5
  • 30
  • 63
3

The triple equals sign === compares both value and type, whereas the double == only compares value

for example "1" and 1 have the same value (so to speak) but are of different types. Therefore the following will occur:

"1" == 1 //true
"1" === 1 //false

This is a great read for some useful javascript knowledge, which includes the triple equals amongst other good-to-know stuff

musefan
  • 47,875
  • 21
  • 135
  • 185
2

The === comparison operator means that the two values won't have their types modified before the comparison is made, so they need to be of the same type as well as representing the same value for it to return true.

'1' == 1 // true
'1' === 1 // false
Anthony Grist
  • 38,173
  • 8
  • 62
  • 76