JavaScript Booleans
JavaScript Booleans
Section titled “JavaScript Booleans”A JavaScript Boolean represents one of two values: true or false. Booleans are fundamental for controlling program flow and making logical comparisons.
Boolean Values
Section titled “Boolean Values”In programming, you often need a data type that can represent a binary state, such as:
- YES / NO
- ON / OFF
- TRUE / FALSE
JavaScript provides the Boolean data type for this purpose. It can only hold the values true or false.
let isJavaScriptFun = true;let isGameOver = false;The Boolean() Function
Section titled “The Boolean() Function”You can use the Boolean() function to explicitly convert any value to its boolean equivalent (to find out if an expression or variable is truthy or falsy).
console.log(Boolean(10 > 9)); // returns trueconsole.log(Boolean(0)); // returns falseconsole.log(Boolean("hello")); // returns trueHowever, explicit conversion with Boolean() is often unnecessary, as JavaScript performs implicit coercion in boolean contexts (like if statements).
if (10 > 9) { // 10 > 9 evaluates to true console.log("This will run.");}Comparisons and Conditions
Section titled “Comparisons and Conditions”Booleans are the cornerstone of comparison operations and conditional statements.
Refer to chapters on JavaScript Comparisons and Conditional Statements for a full overview. Here are some examples:
| Operator | Description | Example |
|---|---|---|
== | Equal to (loose equality, performs type coercion) | if (value == 10) { /* … */ } |
=== | Strictly equal to (no type coercion) | if (value === 10) { /* … */ } |
!= | Not equal to (loose inequality) | if (status != “active”) { /* … */ } |
!== | Strictly not equal to | if (status !== “active”) { /* … */ } |
> | Greater than | if (score > 9000) { /* … */ } |
< | Less than | if (age < 18) { /* … */ } |
>= | Greater than or equal to | if (quantity >= 1) { /* … */ } |
<= | Less than or equal to | if (price <= 50) { /* … */ } |
| The boolean result of an expression (whether it’s truthy or falsy) determines the behavior of conditional logic in JavaScript. |
|---|
Truthy Values
Section titled “Truthy Values”In JavaScript, a value is considered “truthy” if it evaluates to true in a boolean context. Most values are truthy, including:
- The boolean
trueitself. - Any non-empty string (e.g.,
"hello","false"). - Any non-zero number (e.g.,
100,-15,3.14). - Any non-zero
BigInt(e.g.1n). - All objects (including arrays and functions).
// Examples of truthy valuesconsole.log(Boolean(100)); // trueconsole.log(Boolean(3.14)); // trueconsole.log(Boolean(-15)); // trueconsole.log(Boolean("Hello")); // trueconsole.log(Boolean("false")); // true (it's a non-empty string)console.log(Boolean({})); // true (empty object)console.log(Boolean([])); // true (empty array)console.log(Boolean(function(){})); // true (function)Falsy Values
Section titled “Falsy Values”A value is “falsy” if it evaluates to false in a boolean context. There are only a few falsy values in JavaScript:
- The boolean
falseitself. - The number
0(zero). - The number
-0(negative zero). - The
BigInt0n(zero BigInt). - The empty string
""(or''or). null.undefined.NaN(Not a Number).
// Examples of falsy valuesconsole.log(Boolean(false)); // falseconsole.log(Boolean(0)); // falseconsole.log(Boolean(-0)); // falseconsole.log(Boolean(0n)); // falseconsole.log(Boolean("")); // falseconsole.log(Boolean(null)); // falseconsole.log(Boolean(undefined)); // falseconsole.log(Boolean(NaN)); // falseUnderstanding truthy and falsy values is essential for writing concise and correct conditional logic.
Boolean Objects (Avoid)
Section titled “Boolean Objects (Avoid)”Similar to numbers and strings, booleans can be created as objects using new Boolean(). This is generally not recommended.
const primitiveTrue = true;const objectTrue = new Boolean(true);
console.log(typeof primitiveTrue); // "boolean"console.log(typeof objectTrue); // "object"A Boolean object, even if its value is false (e.g., new Boolean(false)), is itself an object and therefore truthy. This can lead to very confusing behavior in conditional statements.
if (new Boolean(false)) { console.log("This runs because new Boolean(false) is an object, which is truthy!");}Always use primitive booleans (true and false) directly. Avoid new Boolean(). |
|---|
Boolean Properties and Methods (via Wrapper Objects)
Section titled “Boolean Properties and Methods (via Wrapper Objects)”Primitive boolean values (true, false) do not have properties or methods. However, JavaScript temporarily wraps them in a Boolean object when you try to access a property or method on them. This allows them to use methods inherited from Boolean.prototype.
The main methods are:
valueOf(): Returns the primitive value of the Boolean object.toString(): Returns a string representing the boolean value ("true"or"false").
const myBool = true;console.log(myBool.toString()); // "true"console.log(myBool.valueOf()); // trueIn practice, you rarely need to call these methods directly on boolean primitives.
Complete Boolean Reference
Section titled “Complete Boolean Reference”For a comprehensive understanding of booleans, truthy/falsy values, and related operators, consult the MDN Web Docs for JavaScript Booleans and Equality comparisons.