This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Smartphone icons created by Freepik - Flaticon
A JavaScript Boolean represents one of two values: true or false.
You can use the
function to find out if an expression (or a variable) is true:Boolean(10 > 9) // returns true
Or even easier:
(10 > 9) // also returns true 10 > 9 // also returns true
Operator | Description | Example |
---|---|---|
== | equal to | if (day == "Monday") |
> | greater than | if (salary > 9000) |
< | less than | if (age < 18) |
The Boolean value of an expression is the basis for all JavaScript comparisons and conditions.
100 3.14 -15 "Hello" "false" 7 + 1 + 3.14
The Boolean value of 0 (zero) is false:
var x = 0; Boolean(x); // returns false
The Boolean value of -0 (minus zero) is false:
var x = -0; Boolean(x); // returns false
The Boolean value of "" (empty string) is false:
var x = ""; Boolean(x); // returns false
The Boolean value of undefined is false:
var x; Boolean(x); // returns false
The Boolean value of null is false:
var x = null; Boolean(x); // returns false
The Boolean value of false is (you guessed it) false:
var x = false; Boolean(x); // returns false
The Boolean value of NaN is false:
var x = 10 / "H"; Boolean(x); // returns false
var x = false; var y = new Boolean(false); // typeof x returns boolean // typeof y returns object
Do not create Boolean objects. It slows down execution speed. The
keyword complicates the code. This can produce some unexpected results:When using the
operator, equal booleans are equal:var x = false; var y = new Boolean(false); // (x == y) is true because x and y have equal values
When using the
operator, equal booleans are not equal, because the operator expects equality in both type and value.var x = false; var y = new Boolean(false); // (x === y) is false because x and y have different types
Or even worse. Objects cannot be compared:
var x = new Boolean(false); var y = new Boolean(false); // (x == y) is false because objects cannot be compared
Eventually the navigation links, above, will be replaced by these (previous) and (next) buttons below.
JavaScript icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon