Booleans

There are only two Boolean values: true and false. They are named after George Boole, an English mathematician who worked in the field of algebraic logic. Boolean values are fundamental in the logical statements that make up a computer program. Every value in JavaScript has a Boolean value and most of them are true (these are known as ‘truthy’ values).

To find the Boolean value of something, you can use the Boolean function like so:


Boolean(“hello”);
<< true


Boolean(42);
<< true


Boolean(0);
<< false
  

Only seven values are always false and these are known as falsy values:

* "" // double quoted empty string
* '' // single quoted empty string
* 0
* NaN
* false
* null
* undefined
  


Truthy and Falsy Values

The fact that empty strings and zero are considered falsy can cause confusion at times, especially since other programming languages don't behave similarly. A ninja needs to be especially careful when dealing with numbers that might be zero, or strings that are empty.

For more on truthy and falsy values, see this article on SitePoint

0 Response to "Booleans"

Post a Comment