JavaScript quirks
Nearly got burnt by a surprising behavior of JavaScript. We probably all know by now that null, the integer 0, '' (the empty string) and undefined are evaluated as false inside an if statement. What's a bit more curious is the following behaviour (try it the firebug console):
false == 0;
// -> true
false == '';
// -> true
false == undefined;
// -> false
false == null;
// -> false
If anyone has a rationale explanation to this, please share.
Comments
-
The Javascript standard explains details in section 11.9.3 how the
==operator is calculated when the two operands have different type.When both operands are String, Number or Boolean, their type is converted to Number, and then the comparison takes place. When both operands are null or undefined, the result of the comparison is
true. However, one when of the operands isnullorundefined, and the other isString,NumberorBoolean, the result of the comparison isfalse.As explained in section 12.5, in the if statement evaluation uses the ToBoolean operator, which is explained in section 9.2 Where it says that
0and''are converted totrue.Note that section 11.12 says that the conditional operator (
?:) also uses the ToBoolean operator.I couldn’t find in the standard any explanation to this semantic difference between the ToBoolean operator and the
==operator whennull/undefinedare involved.Sun, June 17 at 03:59 AM
-
I think trying…
true == undefinedand…true == nullwill go a way to explain it.Sun, June 17 at 08:30 AM
-
I would say so, but I don’t see any such technical constraint from a brief look. However,I am dead tired…
I don’t see why the equation operator couldn’t be defined that when one of the operands is null or undefined, the other operand would need to be converted to Boolean, and the operator would return true if that conversion returned false.
Sun, June 17 at 14:22 PM
-
Thats quite interesting. I suffered from many javascript bugs similar to these in my life :)
Sun, July 08 at 21:53 PM
-
Just out of curiosity, I’ve tried the following:
>>> new Boolean(0) false >>> new Boolean('') false >>> new Boolean(undefined) false >>> new Boolean(null) falseAnd for the lack of optimism ;o)
>>> new Boolean('false') trueSat, July 14 at 10:45 AM
-
Doesn’t “if” behave more like ”!!...” than “false == ...”?
NaN == NaN // false false == NaN // false !!NaN // false if (NaN) {} // falseMon, July 16 at 06:46 AM
-
Just because something evaluates to true/false in the context of a boolean expression doesn’t mean that that thing is therefore equivalent to true/false.
Fri, July 20 at 19:14 PM
-
As an example of what I mean, and continuing what David did:
(!!undefined) === false; (!!null) === false; (!!NaN) === false; (!!false) === false; (!!0) === false; (!!"") === false;(undefined == false) === false; (null == false) === false; (NaN == false) === false; (false == false) === true; (0 == false) === true; ("" == false) === true;Fri, July 20 at 19:20 PM
-
The operator can seem a bit weird at first, but it’s actually quite handy the way it is: if both sides of the operator are of the same type it checks if the arguments are indeed the same, else if the arguments are of a different type it checks if they both amount to the same truthy or falsy value. Use the = operator to do away with the typecheck and just check if the arguments are equal. I would recommend reading some of Douglas Crockford’s articles (http://javascript.crockford.com/), they are definitely worth the time.
Mon, August 06 at 06:01 AM
-
“especially considering 0 and ’’ are seen as equivalent to false“
Not to mention ’ ’ (that’s a space). Also a false value.
Mon, August 06 at 12:49 PM
-
I’m not sure about the evaulation of “undefined”, but the evaluation of “null” makes perfect sense to me. In the database world, null isn’t equal to ANYTHING else, even other nulls. They’ve slackened up on that definition a little in javascript, so null can be equal to null (and also, null == undefined), but the basic principal is still true. Null is neither true NOR false, it’s a non-state.
Thu, August 09 at 08:18 AM
-
A the truthy/falsy fun with JavaScript.Mike has done quite a nice writeup after one of the Douglas Crockford talks in the office: http://www.isolani.co.uk/blog/javascript/TruthyFalsyAndTypeCasting
Thu, August 09 at 15:12 PM
-
I couldn’t find in the Javascript standard(explaining how ”==” operator is calculated when the two operands have different type) any explanation to this semantic difference between the Boolean and == operator whith null/undefined.
Mon, September 03 at 01:52 AM
-
That is interesting :) I’m programing in php at my work so I does’nt have to make declaration of variables. But I remember a few problems when I try to compare variable === variable. It was some freaky results on my script. Anyway thanks for this article !
Thu, September 13 at 22:26 PM