Hatena::Groupmnemonic

きおく屋めがねばし

 | 

2012-11-24

「3.3 論理値」

Boolean Values

A boolean value represents truth or falsehood, on or off, yes or no. There are only two possible values of this type.

undefined
null
0
-0
NaN
""    // the empty string

All other values, including all objects (and arrays) convert to, and work like, true. false, and the six values convert to it, are sometimes called falsy values, and all other values are called truthy. Any time JavaScript expects a boolean value, a falsy value works like false and a truthy value works like true.

if (o !== null) ...
if (o) ...

Boolean values have a toString() method that you can use to convert them to the strings "true" or "false", but they do not have any other useful methods. Despite the trivial API, there are three important boolean operators.

if ((x == 0 && y == 0) || !(z == 0)) {
    // x and y are both zero or z is non-zero
}


トラックバック - http://mnemonic.g.hatena.ne.jp/paragramma/20121124
 |