In javascript there's something one should know about before learning about booleans. That's expressions which is sort of a collection name for all types of code that results in a value. For example the statement x = 5; is an expression. What happens when the variable x is assigned the value is that first the operation on the right side of the assignment operator (=) is evaluated, resulting in 5. An expression has been performed and then is assigned to the variable. In the same way 4 + 6 is an expression where the result is 10.

There are different types of expressions in javascript and the ones described above, where the result is some type of number, are called arithmetic.

Relational expressions

An expression that's good to know about are the relational expressions, which tests values relations with each other and results in either true or false. For example 5 is greater than 4 so results in true, 2 is less than 3, 1 is equal to 1. The result from there types of expressions are always either true or false.

To perform these expressions a couple different operators can be used. The equals operator (==), not equals (!=), identical to (===), not identical to (!==), greater than (>), less than (<), greater or equals to (>=) and lesser or equals to (<=).

var x = 10; // Assignment operator

x == 10; // Equals to 10, true
x == 5; // Equals to 5, false
'bob' === 'bob'; // bob equals to bob, true

x != 9; // Not equals to 9, true
x != 10; // Not equals to 10, false

x === 10; // Identical to 10, true
x !== 7; // Not identical to 7, true

x > 5; // Greater than 5, true
x < 5; // Less than 5, false

15 < 7; // 15 less than 7, false
'def' > 'abc'; // def greater than abc, true

x >= 10; // Greater than or equals to 10, true
x <= 8; // Less than or equals to 8, false

It's usually recommended to use the identical to operator instead of just equals to, because it also compares the data type of the two values. Equal to doesn't so (5 == "5") results in true even though one of the fives is a string. With identical to (5 === "5") results in false.

Booleans in javascript

Above when we've talked about true and false we've actually talked about booleans the whole time. What booleans are in javascript, is a special data type that only can either have the value true or false. In some older languages these are instead 1 or 0.

var bool1 = true;
var bool2 = false;

var bool3 = 5 > 4; // true
var bool4 = 2 === 2; // true
var bool5 = 10 < 7; // false

Booleans are very common in javascript and are many times used to control the data flow in programs. What you do is you set different conditions like for example if something is true do this, else if false do that instead (more about this in the next chapter). Simply said booleans are used as an answer to if something is either true or false.

When using relational expressions and comparison operations the result is always a boolean. For the interested there's more in depth reading on booleans at:

Truthy and falsy values

There's something called truthy and falsy values in javascript. It's nothing complicated, it means that all "real" values that contains something real, like a number or array are truthy values. Other like for example 0, null and undefined, false and empty strings are falsy values.

To test if a value is truthy or falsy the Boolean function can be used.

Boolean(6); // true
Boolean(0); // false
Boolean('Hello'); // true
Boolean(''); // false
Boolean(null); // false
Boolean(NaN); // false
Boolean(true); // true

var x;
Boolean(x); // false

Try it yourself

  1. Try out some different expressions.
  2. Figure out what all falsy values are in javascript.