Boolean: true and false
What you'll learn: How JavaScript uses the boolean type to represent yes/no, on/off, and true/false values.
What is a Boolean?
The boolean type is one of JavaScript's seven primitive types, and it's beautifully simple: it can only be one of two values — true or false. That's it!
Think of a boolean like a light switch. It's either on (true) or off (false). There's no in-between, no "kind of on" or "mostly off." This binary nature makes booleans perfect for representing logical states and making decisions in your programs.
Why Booleans Matter
Booleans are the foundation of logic in programming. They answer questions like:
- Is the user logged in? (
trueorfalse) - Is the value undefined? (
trueorfalse) - Has the form been submitted? (
trueorfalse)
Boolean Values in Action
You can assign boolean values directly:
console.log(true); // true
console.log(false); // false
Notice there are no quotes around true and false — they're not strings like "true" or "false". They're actual boolean values, a distinct primitive type in JavaScript.
True vs "true"
This is a crucial distinction: true (a boolean) is completely different from "true" (a string that happens to contain the word "true"). One represents a logical truth value; the other is just text.
Key Takeaway: Booleans are JavaScript's way of representing logical truth with exactly two possible values: true and false. They're essential for making decisions and controlling program flow.