Skip to Content
JavaScript: The Good Parts
book

JavaScript: The Good Parts

by Douglas Crockford
May 2008
Intermediate to advanced
172 pages
4h 54m
English
O'Reilly Media, Inc.
Content preview from JavaScript: The Good Parts

Falsy Values

JavaScript has a surprisingly large set of falsy values, shown in See Table A-1.

Table A-1. The many falsy values of JavaScript

Value

Type

0

Number

NaN (not a number)

Number

'' (empty string)

String

false

Boolean

null

Object

undefined

Undefined

These values are all falsy, but they are not interchangeable. For example, this is the wrong way to determine if an object is missing a member:

value = myObject[name];
if (value == null) {
   alert(name + ' not found.');
}

undefined is the value of missing members, but the snippet is testing for null. It is using the == operator (see Appendix B), which does type coercion, instead of the more reliable === operator. Sometimes those two errors cancel each other out. Sometimes they don't.

undefined and NaN are not constants. They are global variables, and you can change their values. That should not be possible, and yet it is. Don't do it.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Modern JavaScript for the Impatient

Modern JavaScript for the Impatient

Cay S. Horstmann
Head First JavaScript Programming

Head First JavaScript Programming

Eric T. Freeman, Elisabeth Robson
Learn JavaScript

Learn JavaScript

Shaun Wassell

Publisher Resources

ISBN: 9780596517748Supplemental ContentCatalog PageErrata