Update 2011-12-02: When is it OK to use == in JavaScript?
There are two operators for comparing values in JavaScript: strict equality === and “normal” (or lenient) equality ==. Many style guides (correctly) tell programmers to avoid lenient equality and always use strict equality. This post explains why.
Where appropriate, related sections in the ECMAScript 5 language specification [1] are mentioned in square brackets.
NaN !== _ // any value including NaN x === x +0 === -0for any number x. Thus equality is not reflexive in JavaScript, because NaN is not equal to itself.
> var a = NaN; > a === a false > var b = {}, c = {}; > b === c false > b === b true > "abc" === new String("abc") false // different types (left: primitive, right: object)
> 0 == false true > 1 == true true > 2 == true false > 2 ? true : false true // because 2 !== 0Equality and strings:
> "" == 0 true > "123" == 123 true > "" == false true > "1" == true true > "2" == true false > "true" == true false > "2" ? true : false true // because string is non-empty > "abc" == new String("abc") true // right side converted to primitive