📘 Lesson · Lesson 11
Booleans
Booleans
What Booleans Are
let isLoggedIn = true;
let hasPaid = false;
let isAdult = true;
A boolean holds only one of two values:
true or false. That's it — no other options. Simple as they seem, booleans are the engine of every decision your code makes: should this menu open? Is the user logged in? Did the password match? Every if statement you'll ever write ultimately comes down to a boolean. Note that true and false are written without quotes — "true" in quotes is a STRING, a completely different thing. Naming convention: boolean variables usually start with is, has, or can (isActive, hasPermission), which makes code read naturally.Boolean Expressions — comparisons produce booleans
let age = 20;
console.log(age > 18); // true
console.log(age === 20); // true
console.log(age < 10); // false
let marks = 45;
let passed = marks >= 33; // stores true
console.log(passed); // true
Most booleans in real code aren't typed by hand — they're PRODUCED by comparisons. Whenever you compare two values (
age > 18, name === "Aman"), JavaScript evaluates it and hands back true or false. You can print that result, or store it in a variable as we did with passed. This is the crucial link: comparisons make booleans, and booleans drive if statements. Understanding this connection is the key to control flow, which is the very next group of chapters.Falsy Values — the values that count as false
// These SIX values are "falsy" - they behave like false:
false
0 // the number zero
"" // an empty string
null
undefined
NaN
// Everything else is "truthy"
JavaScript treats certain non-boolean values AS IF they were false — these are called "falsy." There are exactly six, and it's worth memorising them:
false, the number 0, an empty string "", null, undefined, and NaN. Why does this matter? Because you can use a value directly in an if statement: if (userName) means "if userName has any real content." An empty string is falsy, so the check neatly catches "the user typed nothing." This shortcut appears constantly in professional code — and it's a favourite interview question, so learn the list of six.Truthy Values — everything else
// All of these are TRUTHY (behave like true):
"hello" // any non-empty string
"0" // ← even the STRING "0" is truthy! (it's not empty)
"false" // ← the string "false" is truthy too!
42 // any non-zero number
-1 // negatives are truthy
[] // an EMPTY array is truthy!
{} // an EMPTY object is truthy!
Everything that isn't in the falsy list of six is "truthy." Most of this is intuitive — non-empty strings and non-zero numbers behave like true. But three surprises catch people out. The STRING
"0" is truthy (it's a non-empty string, regardless of what it contains), as is the string "false". And an empty array [] or empty object {} is truthy too, even though it holds nothing! To check if an array is actually empty, test its length (arr.length === 0), not the array itself. These three gotchas cause real bugs.Boolean() — checking truthiness explicitly
Boolean("hello"); // true
Boolean(""); // false
Boolean(0); // false
Boolean(42); // true
// The double-NOT shortcut does the same thing:
!!"hello"; // true
!!0; // false
If you want to see whether a value is truthy or falsy, wrap it in
Boolean() — it converts any value into a real true or false. This is handy for testing your understanding, and occasionally for storing a clean boolean. You'll also see the double-NOT trick !!value in professional code, which does exactly the same conversion (the first ! flips it to a boolean and inverts it, the second flips it back). It looks cryptic at first, but now you know: !! just means "convert to a true boolean."Exam Corner
Q: What values can a boolean hold? Only
Q: List the six falsy values. false, 0, "" (empty string), null, undefined, NaN.
Q: Is the string "0" truthy or falsy? Truthy — it's a non-empty string.
Q: Is an empty array [] truthy? Yes! Check
Q: What does Boolean(value) do? Converts any value into true or false based on its truthiness.
true or false.Q: List the six falsy values. false, 0, "" (empty string), null, undefined, NaN.
Q: Is the string "0" truthy or falsy? Truthy — it's a non-empty string.
Q: Is an empty array [] truthy? Yes! Check
arr.length === 0 to test emptiness.Q: What does Boolean(value) do? Converts any value into true or false based on its truthiness.
Booleans क्या हैं
let isLoggedIn = true;
let hasPaid = false;
let isAdult = true;
Boolean सिर्फ दो values में से एक रखता है:
true या false. बस — कोई और विकल्प नहीं. जितने सरल लगते हैं, booleans आपके code के हर फैसले का engine हैं: क्या यह menu खुले? क्या user logged in है? क्या password match हुआ? हर if statement जो आप कभी लिखेंगे आखिरकार boolean पर आती है. ध्यान दीजिए true और false बिना quotes लिखे जाते हैं — quotes में "true" STRING है, बिल्कुल अलग चीज़. Naming convention: boolean variables आमतौर पर is, has, या can से शुरू होते हैं (isActive, hasPermission), जो code को स्वाभाविक पढ़ाता है.Boolean Expressions — comparisons booleans बनाते हैं
let age = 20;
console.log(age > 18); // true
console.log(age === 20); // true
console.log(age < 10); // false
let marks = 45;
let passed = marks >= 33; // true store karta hai
console.log(passed); // true
असली code में ज़्यादातर booleans हाथ से type नहीं होते — वे comparisons से BANTE हैं. जब भी आप दो values की तुलना करें (
age > 18, name === "Aman"), JavaScript उसे evaluate करके true या false लौटाता है. आप उस नतीजे को print कर सकते हैं, या variable में store कर सकते हैं जैसे हमने passed के साथ किया. यही अहम कड़ी है: comparisons booleans बनाते हैं, और booleans if statements चलाते हैं. यह संबंध समझना control flow की चाबी है, जो अगला ही chapter group है.Falsy Values — जो false गिने जाते हैं
// Ye CHHE values "falsy" hain - ye false jaisa vyavhar karti hain:
false
0 // number zero
"" // khali string
null
undefined
NaN
// Baaki sab "truthy" hai
JavaScript कुछ non-boolean values को ऐसे मानता है JAISE वे false हों — इन्हें "falsy" कहते हैं. ठीक छह हैं, और इन्हें याद करना लायक है:
false, number 0, खाली string "", null, undefined, और NaN. यह क्यों मायने रखता है? क्योंकि आप value को सीधे if statement में use कर सकते हैं: if (userName) मतलब "अगर userName में कोई असली content है." खाली string falsy है, तो जांच साफ-साफ पकड़ती है "user ने कुछ type नहीं किया." यह shortcut professional code में लगातार आता है — और पसंदीदा interview सवाल है, तो छह की list सीखिए.Truthy Values — बाकी सब
// Ye sab TRUTHY hain (true jaisa vyavhar):
"hello" // koi bhi non-empty string
"0" // ← STRING "0" bhi truthy hai! (khali nahi hai)
"false" // ← string "false" bhi truthy hai!
42 // koi bhi non-zero number
-1 // negatives truthy hain
[] // KHALI array truthy hai!
{} // KHALI object truthy hai!
जो कुछ छह की falsy list में नहीं वह "truthy" है. इसमें ज़्यादातर सहज है — non-empty strings और non-zero numbers true जैसा व्यवहार करते हैं. पर तीन आश्चर्य लोगों को फंसाते हैं. STRING
"0" truthy है (यह non-empty string है, चाहे उसमें कुछ भी हो), वैसे ही string "false". और खाली array [] या खाली object {} भी truthy है, भले ही उसमें कुछ न हो! Array असल में खाली है जांचने को उसकी length test कीजिए (arr.length === 0), array खुद नहीं. ये तीन gotchas असली bugs पैदा करते हैं.Boolean() — truthiness साफ जांचना
Boolean("hello"); // true
Boolean(""); // false
Boolean(0); // false
Boolean(42); // true
// Double-NOT shortcut vahi karta hai:
!!"hello"; // true
!!0; // false
अगर देखना है कोई value truthy है या falsy, उसे
Boolean() में लपेटिए — यह किसी भी value को असली true या false में बदलता है. यह अपनी समझ जांचने को काम का है, और कभी-कभी साफ boolean store करने को. आप professional code में double-NOT trick !!value भी देखेंगे, जो ठीक वही conversion करता है (पहला ! इसे boolean में बदलकर उलटता है, दूसरा वापस पलटता है). पहले यह रहस्यमय लगता है, पर अब आप जानते हैं: !! बस मतलब "असली boolean में बदलो."Exam Corner
Q: Boolean कौन-सी values रख सकता है? सिर्फ
Q: छह falsy values बताइए. false, 0, "" (खाली string), null, undefined, NaN.
Q: क्या string "0" truthy है या falsy? Truthy — यह non-empty string है.
Q: क्या खाली array [] truthy है? हां! खालीपन जांचने को
Q: Boolean(value) क्या करता है? किसी भी value को उसकी truthiness के आधार पर true या false में बदलता है.
true या false.Q: छह falsy values बताइए. false, 0, "" (खाली string), null, undefined, NaN.
Q: क्या string "0" truthy है या falsy? Truthy — यह non-empty string है.
Q: क्या खाली array [] truthy है? हां! खालीपन जांचने को
arr.length === 0 देखिए.Q: Boolean(value) क्या करता है? किसी भी value को उसकी truthiness के आधार पर true या false में बदलता है.
💻 Live Code Editor
इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.
सब कुछ आपके browser में ही चलता है — कोई server, कोई signup नहीं. JavaScript का
console.log देखने के लिए F12 दबाइए.