Web Development - JavaScript Booleans
JavaScript Booleans
Booleans represent one of two values: true or false.
They are often used in conditional testing to decide what should happen in your program.
Boolean Values
A Boolean value is either true or false:
Comparison Returns a Boolean
When you compare values, the result is a Boolean value:
Example
let x = 10;
let y = 5;
document.getElementById("demo1").innerHTML
= (x > y); // true
document.getElementById("demo2").innerHTML = (x
== y); // false
Try it Yourself »
Booleans in Conditions
Booleans are used in if statements and loops
to control flow, which you will learn about in the next chapter.
Example
let isDay = true;
if (isDay) {
document.getElementById("demo").innerHTML = "Good morning!";
} else {
document.getElementById("demo").innerHTML = "Good night!";
}
Try it Yourself »
Summary
- Booleans represent
trueorfalse. - Comparisons and conditions return Booleans.
Next: Learn about JavaScript Conditions to see how Booleans control your program's flow.