Booleans in javaScript what to know

In javaScript one of the most important primitive values to work with is booleans. To create a boolean there is the boolean literal, the Boolean object. In addition booleans can happen as a result of an expression as well. There are some tricks that I have picked up here and there when it comes to booleans, so I will be sure to write about everything that I know about in this post when it comes to Booleans with javaScript.

1 - What to know

This is not a getting started post on javaScript, in this post I am writing just about booleans and how they are used with programming tasks with javaScript. If you are new to javaScript you might want to start with my getting started post on javaScript.

2 - A Boolean literal

For the most part if I want to set a boolean value I just set it using a literal. The true and false boolean literals can be used do do just this. For this example I have a boolean called firstRun that is set to true, I then also have a loop that will fire once every second by way of using setTimeout. The first time that the loop fires, a ‘first run’ message will logg, and the firstRun bool will set back to false.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var firstRun = true;
var loop = function () {
setTimeout(loop, 1000);
if (firstRun) {
console.log('first run!');
firstRun = false;
}
console.log('tick');
}
loop();

3 - Booleans from expressions

Boolean values can also be the result of an expression. For example say I have a x variable that holds a number value and I want another boolean that will be true when the x variable is in a certain range.

1
2
3
var x = 5;
var inRange = x > 4 && x < 10;
console.log(inRange); // true

3.1 - Using !0 and !1

many projects that aim to make the source code as compact as possible take advantage of all kinds of tricks to reduce file size. Sometimes I see the use of the expression !0 to replace the boolean literal true.

1
2
var g = !0; // true
var b = !1; // false

This works because the number 0 evaluates to false, and the ! operator both converts to boolean and negates the value as well. For projects where I really do want to crunch down file size it might be called for, but it reduces readability for some developers as well.

3.2 - Using !!

So the ! operator converts a non boolean value to a boolean, but it is also negated. So just calling the operator twice will then negate it back to its original value.

1
2
3
var a = !!''; // false
var b = !!'foo'; // true
var c = !!1; // true

4 - The Boolean Object

In javaScript there is the Boolean global object that can be used as a constructor with the new keyword, or not. When used with the new keyword it will return an object and not a primitive Boolean value. The primitive value of the object can be retrieved with the valueOf method, but I can not think of much of any reason to create a Boolean variable this way.

4.1 - Using the Boolean Object as a constructor

When using the Boolean Object as a constructor it returns an object, and not a boolen. However the valueOf method can be used to get the boolean value of the Object.

1
2
var b = typeof new Boolean(false); // 'object'
var c = typeof new Boolean(false).valueOf(); // 'boolean'

Making booleans this way is not such a great idea. It makes doing so far more complicated than it needs to be, and can lean to unexspected results if you are not aware of the fact that an object evaluates to true.

1
2
3
4
5
6
var foo = new Boolean(false),
n = 0;
if (foo) {
n += 1;
}
console.log(n); // 1

4.2 - Using the Boolean Object as a method

When omitting the new keyword a boolean primitive value will be returned rather than an object, making it a way to convert to a boolean.

1
var a = Boolean(null); // false

I do not use this as well, because the !! operator works just fine to get such a task done. Still the Boolean Object is something to be aware of as it is often used in examples.