JavaScript Booleans — Interview Questions & Answers
Ques: What is a Boolean in JavaScript?
Ans: A Boolean is a data type that can have only two values: true or false.
Booleans are mainly used for conditional testing, logical operations, and control flow.
Example:
let isOnline = true;
let hasAccess = false;
Ques: What is the type of a Boolean value?
typeof true; // "boolean"
typeof false; // "boolean"
Ques: What values are considered truthy or falsy in JavaScript?
JavaScript automatically converts values to Boolean when needed (type coercion).
| Falsy Values | Truthy Values |
|---|---|
false | Everything not falsy |
0 | true |
-0 | 42 |
"" (empty string) | "hello" |
null | [], {} |
undefined | "0", "false" |
NaN | any non-empty object |
Example:
if (0) console.log("This won’t run");
if ("hello") console.log("This will run");
Ques: How can you convert a value to Boolean explicitly?
-
Using the Boolean() function
Boolean(10); // true Boolean(""); // false -
Using double NOT (!!) operator
!!"text"; // true !!0; // false
Ques: What is the difference between Boolean object and Boolean primitive?
| Feature | Boolean Primitive | Boolean Object |
|---|---|---|
| Created with | true, false | new Boolean(value) |
| Type | "boolean" | "object" |
| Example | typeof true → "boolean" | typeof new Boolean(false) → "object" |
Example:
let x = false;
let y = new Boolean(false);
console.log(typeof x); // "boolean"
console.log(typeof y); // "object"
Boolean objects are truthy, even if the value is false.
if (new Boolean(false)) console.log("This runs!"); // true
Ques: How are Booleans used in conditions?
Ans: Booleans are used to control the flow of code in if-else, loops, and ternary operators.
let isLoggedIn = true;
if (isLoggedIn) {
console.log("Welcome user!");
} else {
console.log("Please login.");
}
Ques: How do you compare Boolean values?
true == 1; // true (loose equality)
true === 1; // false (strict equality)
false == 0; // true
false === 0; // false
Ques: How to check if a variable is Boolean?
typeof value === "boolean"; // true or false
Ques: What is Boolean coercion in JavaScript?
Ans: When JavaScript automatically converts other data types into Boolean values in a logical context.
Example:
if ("hello") console.log("Truthy"); // "hello" → true
if (0) console.log("Falsy"); // 0 → false
Ques: What are common Boolean operations in JS?
| Operator | Description | Example | Result |
|---|---|---|---|
&& | Logical AND | true && false | false |
| ` | ` | Logical OR | |
! | Logical NOT | !true | false |
Ques: What is short-circuit evaluation?
Ans: JS stops evaluating a logical expression as soon as the result is known.
Example:
false && console.log("Skipped"); // Skipped is NOT printed
true || console.log("Skipped"); // Skipped is NOT printed
Ques: What’s the difference between Boolean(value) and !!value?
Both convert a value to Boolean, but:
| Method | Output Type | Example |
|---|---|---|
Boolean("Hi") | true | Converts directly |
!!"Hi" | true | Uses NOT twice for coercion |
Ques: Can Booleans be used in arithmetic operations?
Ans: Yes, In numeric context, true → 1 and false → 0.
Example:
console.log(true + true); // 2
console.log(false + 5); // 5
Ques: How do you toggle a Boolean value?
let flag = true;
flag = !flag; // toggles between true ↔ false
Ques: What is Boolean casting in JavaScript?
Ans: Casting means converting a non-boolean value into Boolean using:
Boolean(value);
!!value;
Ques: What’s the difference between null, undefined, and false in Boolean context?
| Value | Boolean Result |
|---|---|
null | false |
undefined | false |
false | false |
0 | false |
Ques: What happens if you use Boolean in string concatenation?
Ans: Booleans are automatically converted to strings when concatenated.
"Result: " + true; // "Result: true"
Ques: How are Booleans stored internally?
Ans: Booleans are stored as binary values — 1 for true, 0 for false.
