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.
var bool1 = true;
var bool2 = false;
var bool3 = 5 > 4; // true
var bool4 = 2 === 2; // true
var bool5 = 10 < 7; // false
Truthy and 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
Boolean(x); // false
Try it yourself
- Try out some different expressions.