Very often, in programming, you will need a data type that can only have one of two values, like
The Boolean() Function
You can use the Boolean() function to find out if an expression (or a variable) is true:
Or even easier:
Comparisons and Conditions
The chapter JS Comparisons gives a full overview of comparison operators.
The chapter JS Conditions gives a full overview of conditional statements.
Here are some examples:
|==||equal to||if (day == "Monday")|
|>||greater than||if (salary > 9000)|
|<||less than||if (age < 18)|
Everything With a "Real" Value is True
Everything Without a "Real" is False
The Boolean value of 0 (zero) is false:
The Boolean value of -0 (minus zero) is false:
The Boolean value of "" (empty string) is false:
The Boolean value of undefined is false:
The Boolean value of null is false:
The Boolean value of false is (you guessed it) false:
The Boolean value of NaN is false:
Boolean Properties and Methods
Primitive values, like true and false, cannot have properties or methods (because they are not objects).
Complete Boolean Reference
The reference contains descriptions and examples of all Boolean properties and methods.