To perform a strict checking, you should use the strict comparison operator "triple equals", and you should be aware that by default the loose "double equals" operator is used.
There is a bunch of values that are loosely evaluate as false (friendly known as "falsy"):
var myUndef, // undefined myNull = null, myFalse = false, myEmpty = '', // empty string myZero = 0, // numeric zero myNan = parseInt('not a number'); // parsing a non numeric string returns NaN if(myUndef || myNull || myFalse || myEmpty || myZero || myNan) // 1 console.log('Unexpected'); try { if(myNotExisting) // 2 console.log('Unexpected'); } catch(e) { console.log(e); } if('false') { // 3 console.log('Misleading'); }1. All these values (undefined, false, empty string, zero, NaN) are loosely equals to the boolean false value, so the next line is not executed.
2. This variable was even not declared, checking it results in an exception of type ReferenceError.
3. The string 'false' is not converted to the boolean false value, and it is evaluated to true.
No comments:
Post a Comment