JavaScript Logic
- Previous Page JS Random
- Next Page JS Comparison
JavaScript boolean (logical) represents one of two values:true
or false
.
Boolean values
In programming, you often need a data type that can only have one of two values, such as
- YES / NO
- ON / OFF
- TRUE / FALSE
For this reason, JavaScript provides aBooleandata types. It only accepts values true or false.
Boolean() function
You can use Boolean()
A function to determine whether an expression (or variable) is true:
Example
Boolean(10 > 9) // returns true
Or even simpler:
Example
(10 > 9) // also returns true 10 > 9 // also returns true
Comparison and Condition
This chapter of JS Comparison lists the complete comparison operators.
This chapter of JS Conditions lists the complete conditional statements.
Here are some examples:
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 of JavaScript comparison and conditional logic.
All values that have a 'truthy' value are True
Example
100 3.14 -15 "Hello" "false" 7 + 1 + 3.14 5 < 6
All values that do not have a 'truthy' value are False
0 (zero)The boolean value is false:
var x = 0; Boolean(x); // returns false
-0 (negative zero)The boolean value is false:
var x = -0; Boolean(x); // returns false
"" (empty value)The boolean value is false:
var x = ""; Boolean(x); // returns false
undefined The boolean value is false:
var x; Boolean(x); // returns false
null The boolean value is false:
var x = null; Boolean(x); // returns false
false The boolean value (as you might have guessed) is false:
var x = false; Boolean(x); // returns false
NaN The boolean value is false:
var x = 10 / "H"; Boolean(x); // returns false
Boolean can be an object
Typically, JavaScript booleans are created from primitive values:
var x = false
But boolean can also be defined through the keyword new
Defined as an object:
var y = new Boolean(false)
Example
var x = false; var y = new Boolean(false); // typeof x returns boolean // typeof y returns object
Do not create boolean objects. It will slow down the execution speed.
new
The keyword can complicate the code and produce some unexpected results:
when using ==
operator, equal booleans are equal:
Example
var x = false; var y = new Boolean(false); // (x == y) is true because x and y have equal values
when using ===
When using the operator, equal booleans are not equal because ===
The operator needs to be equal in both type and value.
Example
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:
Example
var x = new Boolean(false); var y = new Boolean(false); // (x == y) is false because objects cannot be compared
Comparing two JavaScript objects will always return false.
Complete Boolean Reference Manual
For the complete reference manual, please visit our JavaScript Boolean Reference Manual.
The reference manual includes descriptions and examples of all boolean properties and methods.
- Previous Page JS Random
- Next Page JS Comparison