Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need an example explaining why use of comparison operands != and == is considered bad practice in JavaScript

Tags:

javascript

As I've read somewhere it is advised to use !== and === instead.

like image 573
z-boss Avatar asked Oct 23 '08 18:10

z-boss


2 Answers

"Use the strict equality operator (===) when you want to check that the two operands are of the same type and value. Use the regular equality operator (==) if you care only about the value and the type does not matter. If, for example, one operand is the number 5 and the other operand is the string "5", standard equality operator will return true, but, since they are not of the same type, a strict equality operator will return false." http://www.webreference.com/js/column26/stricteq.html

like image 164
Totty Avatar answered Nov 02 '22 23:11

Totty


It depends on what your'e trying to do.

!== and === compare both value and type.

5 == "5"

The above will return true. If that's not what you intended, you should do this instead:

5 === "5"

That would return false.

like image 44
Joel Coehoorn Avatar answered Nov 02 '22 22:11

Joel Coehoorn