Skip to main content

Shades of Truth and Falsity

Earlier, I mentioned that you never need new Boolean to create a boolean value. That's because Javascript rarely needs boolean values at all; it will gladly make boolean decisions based on any type of variable--numbers, strings, even objects!

When used in a context that needs a boolean value, Javascript considers these values--and only these values--to be false:

false (the boolean kind)
0 (numeric zero)
null (the null value)
undefined (the undefined value)
NaN (the not-a-number value)
"" (an empty string)

Anything else is considered to be a true value. Anything. Really. Memorize the entire set of potentially false values, it will make your Javascript better. Be careful, Javascript will not automagically convert values from another type when it evaluates their booleanosity. String values of "0", "false", or "null" evaluate to true. In contrast, an expression like parseInt("0") returns numeric zero and evaluates to false. Then again, parseInt("null") returns NaN (it's not a number, obviously) and that is also false.

One common use of the undefined value is to check for the existence of properties in an object. Thus, you'll often see if ( document.getElementById ) to check browser support for a DOM function. The value will either be undefined or a function object, which are interpreted as false/true respectively. Note that this trick won't work to check for undefined variables. If a variable has never been defined, Javascript will give you an error. The safe way to check for a variable that has never been declared is to use typeof(nosuchvar)=="undefined".

It's worth learning the boolean drill backwards and forwards, because knowing these rules comes in mighty handy with the next topic: short-circuit evaluation.

Comments

Popular posts from this blog

Tragedy of the WebKit Commons

With Opera announcing that their future products will be based on WebKit, the Internet is abuzz with discussion about what that means and whether it's a good thing. Looking at it as a jQuery developer, it's a good thing if it gets WebKit participants to fix bugs and update older implementations. I can't be optimistic without some evidence that things are really going to change. We don't know how many of Opera's core developers will move to WebKit development, but the press release isn't encouraging:  "The shift to WebKit means more of our resources can be dedicated to developing new features and the user-friendly solutions."  I suspect they want some cost savings by eliminating Presto technical staff, or -- in the most optimistic case for their employees -- refocusing existing staff on the parts outside WebKit core that make browsers different. Opera did  land their first WebKit patch  so they wanted to make a statement that they weren't gettin...

Please Stop the jsPerf.com Abuse!

According to many of the tests on jsperf.com , jQuery is slow. Really slow. The jQuery team gets bug reports from web citizens who've discovered these egregious flaws via jsperf and want them fixed. Now, jsperf.com can be a great tool when used knowledgeably, but its results can also be misinterpreted and abused. For example: Why doesn't jQuery use DOM classList for methods like .addClass()? There are at least three good reasons. It doesn't make any practical performance difference given its frequency of use. It complicates code paths, since classList isn't supported everywhere. It makes jQuery bigger, which makes it load slower for everyone, every time. But this jsperf shows classList is much faster! How can you say that? Well it's possible that test is running afoul of microbenchmark issues , and not measuring what it is supposed to measure due to the increased sophistication of today's JavaScript compilers. This is a big problem with a lot of t...

You Might Not Need Babel

Babel is great, and by all means use it if it makes it easier to develop your application. If you're developing a library on the other hand, please take a moment to consider whether you actually need Babel. Maybe you can write a few more lines of utility code and forego the need to pre-process. If you're only targeting some platforms, you may not need anything more than plain old ES6. At the very least, make sure you know what Babel is doing for you, and what it's not. Some developers believe that Babel is protecting us from a great demon of code complexity when, in truth, browsers and node are pretty easy to deal with on their own. Let's take a look at some of the strategies for not needing Babel. ES5 is pretty okay: In the days when we used tables for layout and the debugger was named alert() , we would have been so grateful if JavaScript had the features that ES5 has today. If you truly need Babel you are not only a weak programmer, but also an ingrate. Face ...