Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

equality in javascript [duplicate]

Tags:

javascript

When working within javascript can someone provide me a good reference or explanation over testing for equality/inequality and type coercion?

From what I have been reading I see where there are two principles of thought on using eqeq (==) vs. eqeqeq (===) some feel that you should not use eqeq and always as use eqeqeq as it is safer to use.

I have been playing around with some basic samples and I am having trouble discerning the difference or when best to use one over the other:

For example: here is some basic script I was writing out. When I test using eqeq or eqeqeq I get the same result. I have not seen an example yet where I would get a different results (ie. using eqeq returns true where eqeqeq returns false).

function write(message){

  document.getElementById('message').innerHTML += message +'<br/>';
}


var tim = { name: "tim" };
var tim2 = { name: "tim" };
//objects are equal to themselves ( == vs ==== eqeq or eqeqeq)
write("tim eq tim: " + (tim == tim)); //returns true

//objects are only equal to themselves regardless of containing value got that
write("tim eq tim2: " + (tim === tim2)); //returns false

//access the primative type to test true or false
write("tim value eq tim2 value: " + (tim.name === tim2.name)); //returns true
//how does this differ in efficency over the eqeq operator? is one safer to use over the other?
//write("tim value eq tim2 value: " + (tim.name == tim2.name)); //also returns true

//testing primatives

write("apple eqeqeq apple: " + ("apple" === "apple"));  //true
write("apple eqeqeq apple: " + ("apple" == "apple"));  //true

Can someone provide an explanation or reference I can read that helps clarify this a bit more.

cheers,

like image 502
rlcrews Avatar asked Mar 17 '12 15:03

rlcrews


1 Answers

The difference between == and === is fairly simple: == is a comparison of value. === is a comparison of value and type. Using === will prevent JavaScript from dynamically determining type and compares values exactly as they are.

5 == "5"     //true - JS compares the number 5 to a string of "5" and determines the contents are the same 
5 === "5"    //false - A character is not a number, they can't be the same.

0 == false   //true - false is a bool, 0 is numerical, but JS determines that 0 evaluates to false
0 === false  //false - numeric is not boolean, they can't be exactly the same thing

5 == 5.0     //true - need I say more?
5 === 5.0    //true - one is a float and one is an int, but JS treats them all as numerical

I find it's critical for tests with functions that can return both false (for failure) and 0 (as a legitimate result).

JS has a total of 5 primitive types = numerical, string, boolean, null and undefined. === requires both arguments to be of the same type and equal value to return true. There are no float, int, long, short, etc - any type of number is lumped together as numerical.

like image 147
Surreal Dreams Avatar answered Oct 11 '22 00:10

Surreal Dreams