JavaScript Making Decisions

Non-Boolean Results

Earlier we looked at how JavaScript converts between numbers and text strings in order to be able to process operations that included both. Similarly when a Boolean value is expected and a different data type is supplied that value will be converted into a Boolean in order for the result to be usable.

With very few exceptions, such as number variables containing zero and text string variables that contain a completely empty string, most values that variables can hold are considered to be true:

if (1) { . . .  true
if (0) { . . . false
if ("") { . . . false
if (" ") { . . . true
if {"0") { . . . true
if (undefined) { . . . false
if (null) { . . . false

The last two of these, where a variable evaluates to undefined or null, allow us to perform what is called feature sensing. This allows us to hide parts of our JavaScript code from those browsers that do not support the specific features that are required for the code to work correctly.

go to previous section go to next section 


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow