JavaScript

  1. Home
  2. Computing & Technology
  3. JavaScript

JavaScript Making Decisions

7. Non-Boolean Results

By Stephen Chapman, About.com

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.

This tutorial first appeared on www.felgall.com and is reproduced here with the permission of the author.

Explore JavaScript

About.com Special Features

JavaScript

  1. Home
  2. Computing & Technology
  3. JavaScript
  4. Javascript Tutorials
  5. Learn Modern JavaScript
  6. 2. Decision Making
  7. JavaScript Non-Boolean Results

©2009 About.com, a part of The New York Times Company.

All rights reserved.