completefrontend
Open lab My progress
THE JAVASCRIPT FIELD GUIDE

Booleans & truthiness

Learn true and false, compare values, convert truthy and falsy inputs, avoid == true traps, and use booleans in practical JavaScript.

By the end, you can
  • 01
    Read boolean answersKnow the two booleans, true and false, and spot the booleans that comparisons, yes/no methods, and ! give you.
  • 02
    Predict truthy and falsyUse the list of eight falsy values. Explain why values like "false", [], and new Boolean(false) are truthy.
  • 03
    Write safer checksTurn values into booleans on purpose, avoid == true, and pick the right check for toggles, lists, forms, and text from web addresses.

true and false

A boolean is a yes/no value. There are only two booleans: true means yes, and false means no. Booleans are primitive values: simple values, like numbers and strings.

You will often keep booleans in variables with names like isOpen, hasItems, isLoggedIn, and checked.

The two boolean valuesPop out in the code editor (opens in a new tab)JavaScript
const isOpen = true;const hasItems = false;console.log(typeof true);console.log(isOpen);console.log(hasItems);

Line 1 stores true in isOpen. Line 2 stores false in hasItems. Line 3 uses typeof, which gives the type of a value as text. It prints "boolean", because true is a boolean, not a string. Lines 4 and 5 print the two stored values.

You can give these variables any name. But names that start with is, has, or can read like yes/no questions, so the code is easy to read.

Definition

A boolean is exactly true or false. Any other value can stand in for a boolean when JavaScript needs a yes/no answer, for example in an if. A value is truthy when JavaScript treats it like true. A value is falsy when JavaScript treats it like false. Whether a value is truthy or falsy is called its truthiness.

Real-life analogyA short no list at a party door

A guard stands at a party door. The guard has a short list of names that may not come in. If your name is on that list, the answer is no. Everyone else comes in. The guard only checks the list. It does not matter what else is written on your name tag.

In real life: The few names on the no list
In JavaScript: The eight falsy values
In real life: Everyone else comes in
In JavaScript: Every other value is truthy
In real life: A name tag that says No still gets in
In JavaScript: The string "false" is truthy

This lesson is about using booleans. To see how the engine stores them, read Booleans inside the engine. Later, the Booleans, null & undefined lesson puts booleans next to null and undefined, the two values that mean "nothing here".

Where booleans come from

STEP THROUGH

You can write true and false yourself. But most booleans come from questions that your code asks:

  • A comparison, such as 5 > 3, asks how two values compare.
  • Some methods ask a yes/no question, such as "does this text contain ell?"
  • The not operator, !, gives the opposite answer.
Step through where booleans come from
Step 0 of 14Ready
Your turn: follow the blue line

Step through the places where booleans come from: values you write, comparisons, yes/no methods, and !.

Running in
  1. script
Next: line 1
const hasItems = false;const trueType = typeof true;const bigger = 5 > 3;const sameLetter = "a" === "A";const hasEll = "hello".includes("ell");const isArray = Array.isArray([]);const isReallyNaN = Number.isNaN(NaN);const startsWithC = "cat".startsWith("c");const isClosed = !isOpen;console.log(isOpen, hasItems, trueType);console.log(bigger, sameLetter);console.log(hasEll, isArray, isReallyNaN, startsWithC);console.log(isClosed);
CallStoreChangeResultBlue line: runs next. Ran: already executed.
Recent returnsNothing yet. Start with the blue line.
A guided replay recorded from real JavaScript calls, not an engine debugger. Step follows executed statements; Back reviews a snapshot. Reset starts a fresh run.

Read each variable name as a question:

  • bigger asks: is 5 more than 3? Yes, so it holds true.
  • sameLetter asks: is "a" the same as "A"? No, because small and capital letters are different. So it holds false.
  • hasEll asks: does "hello" contain "ell"? Yes.
  • isArray asks: is [] an array? Yes.
  • isReallyNaN asks: is this value NaN, the "not a number" value? Yes.
  • startsWithC asks: does "cat" start with "c"? Yes.
  • isClosed uses ! to get the opposite of isOpen. isOpen is true, so isClosed is false.
Three questions and their answers. 5 > 3 gives true. "a" === "A" gives false. "cat".startsWith("c") gives true.questionanswer5 > 3true"a" === "A"false"cat".startsWith("c")true
Each card on the left asks a yes/no question. JavaScript answers each one with a boolean: true for yes, false for no.

Turning a value into a boolean

STEP THROUGH

Sometimes you have a value and want to know: will JavaScript treat it as true or as false? Finding out is called boolean conversion. There are two common ways to get the answer:

  • Boolean(value) gives the answer as true or false. This is the clearest way.
  • !!value gives the same answer. It uses the not operator ! two times.
Step through: turning a value into a boolean
Step 0 of 7Ready
Your turn: follow the blue line

Compare Boolean(value), !!value, !value, and the block that if (value) runs.

Running in
  1. script
Next: line 1
const explicit = Boolean(value);const compact = !!value;const flipped = !value;if (value) {  console.log("truthy branch");} else {  console.log("falsy branch");}console.log(explicit, compact, flipped);
CallStoreChangeResultBlue line: runs next. Ran: already executed.
Recent returnsNothing yet. Start with the blue line.
Value to test

Pick a different value to start again from line 1. Watch line 4: one ! gives the opposite answer.

A guided replay recorded from real JavaScript calls, not an engine debugger. Step follows executed statements; Back reviews a snapshot. Reset starts a fresh run.

An if (value) does the same conversion to choose which block of code to run. None of these change value itself. They only answer a yes/no question about it.

Why does !! work? One ! gives the opposite answer. A second ! gives the opposite of that, which is the plain answer again.

The empty string "" goes through one ! and becomes true. A second ! turns it into false. So !!"" is false, the same as Boolean("").""!true!falseempty stringafter one !after two !!!"" is false
The empty string is falsy. The first ! gives the opposite, true. The second ! flips it back to false. So !!value gives the same answer as Boolean(value).

The eight falsy values

Only eight values are falsy. Learn this list by heart. Every other value is truthy.

  • false
  • 0
  • -0
  • 0n
  • ""
  • null
  • undefined
  • NaN
A strange browser case: document.all

Browsers have one old object, document.all, that is falsy even though it is an object. Browsers keep this rule only so that very old web pages still work. The rule is in a part of the specification called Annex B, under the name [[IsHTMLDDA]]. You will not need it in your own code. If you are curious, Null inside the engine explains it.

The surprises are all on the truthy side. These values look empty or false, but they are truthy:

  • The strings "0", "false", and " " (one space).
  • An empty array, [], and an empty object, {}.
  • Functions and symbols.
  • Wrapper objects, such as new Boolean(false). You'll meet them later in this lesson.
Left: the eight falsy values, false, 0, -0, 0n, the empty string, null, undefined, and NaN. Right: values that only look empty or false, "0", "false", " ", "null", [], and {}. They are all truthy, like every other value.falsy: only these 8false0-00n""nullundefinedNaNtruthy: all the rest"0""false"" ""null"[]{}and every other value
The left side is the whole falsy list. The right side shows values that only look empty or false. They are text or objects, so they are truthy, like every value not on the left.

The quotes matter. "0" and "false" are strings with characters in them, so they are not empty. [] and {} are objects, and objects are truthy. JavaScript does not read the words inside a string. So Boolean("0"), Boolean("false"), Boolean([]), and Boolean({}) all give true.

Truthiness playground

48 VALUES

Pick a value from the list and see how JavaScript treats it. The playground shows three results: Boolean(value), !!value, and which block if (value) runs.

The last group, "Computed expressions", holds common bugs. For example, "abc".indexOf("a") gives 0, because "a" is at position 0, and 0 is falsy. And 0.1 + 0.2 - 0.3 leaves a tiny number that is not quite zero, so it is truthy.

Truthiness playground
Test the value you pickedPop out in the code editor (opens in a new tab)JavaScript
const value = "0";console.log(Boolean(value));console.log(!!value);if (value) {  console.log("truthy branch");} else {  console.log("falsy branch");}
Resultstruthy
Code"0"
Value"0"
Boolean(value)true
!!valuetrue
if runstruthy branch

A string with characters in it is truthy, even when the character is 0.

Try it yourself

"0" is truthy. Boolean(value) is true, !!value is true, and if (value) runs the truthy branch. A string with characters in it is truthy, even when the character is 0.

Each result comes from the real JavaScript value. Line 1 of the code changes to the value you pick.
Truthy or falsy sorter
  • false
  • 0
  • -0
  • 0n
  • ""
  • null
  • undefined
  • NaN
  • "0"
  • "false"
  • " "
  • []
  • {}
  • [""]
  • [[]]
  • function () {}
  • Symbol()
  • 1n
  • -1
  • Infinity
  • new Boolean(false)
  • new Number(0)
  • new String("")
  • /(?:)/
Try it yourself
0 of 24 correct

Put each card in the right group. Watch out: empty arrays, empty objects, and wrapper objects are truthy.

Choose a category for every card. You can change an answer at any time; Reset clears them all.

The table below lists every value from the playground, in the same groups. Read each row as: this value is truthy or falsy, and here is why. The middle column is worked out from the real value when the page is built, so it is always right.

All 48 playground values, truthy or falsy
The valueTruthy or falsy?Why
truetruthytrue is already a boolean, so it stays true.
falsefalsyThe boolean false is falsy by definition.
nullfalsynull means "empty on purpose". It is on the falsy list.
undefinedfalsyundefined means "no value was given". It is on the falsy list.
void 0falsyThe void operator always gives undefined, so this is falsy too.
""falsyOnly the string with zero characters is falsy.
"0"truthyA string with characters in it is truthy, even when the character is 0.
"false"truthyJavaScript does not read the word inside the string. The string is not empty, so it is truthy.
" "truthyA space is a character, so the string is not empty.
"\n"truthyA new line counts as one character, so the string is not empty.
"null"truthyThis is text, not the value null. The text is not empty, so it is truthy.
"undefined"truthyThis is text that spells undefined. The text is not empty, so it is truthy.
"NaN"truthyThis is text, not the number value NaN. The text is not empty, so it is truthy.
0falsyNumber zero is falsy.
-0falsyNegative zero is still zero for truthiness.
NaNfalsyNaN means "not a number". It is on the falsy list.
-1truthyAny number that is not zero is truthy, even a negative one.
0.1truthyAny decimal number that is not zero is truthy.
InfinitytruthyInfinity is a number that is not zero, so it is truthy.
-Infinitytruthy-Infinity is a number that is not zero, so it is truthy.
0nfalsy0n is zero as a BigInt, a number type for very big whole numbers. It is falsy.
1ntruthyA BigInt that is not zero is truthy.
-1ntruthyA negative BigInt is not zero, so it is truthy.
[]truthyAn array is an object, and objects are truthy.
({})truthyAn object is truthy, even when it holds nothing.
[0]truthyThe array is truthy, even though the item inside it is falsy.
[""]truthyThe array is an object, so it is truthy. The falsy item inside does not matter.
[[]]truthyThis is an array with an empty array inside. The outer array is an object, so it is truthy.
function () {}truthyA function is an object, and objects are truthy.
() => {}truthyAn arrow function is a function, so it is truthy.
Symbol()truthyEvery symbol is truthy.
new Date(0)truthyA date is an object, so it is truthy.
/(?:)/truthyA regular expression, a pattern for searching text, is an object. So it is truthy.
new Boolean(false)truthyThis makes an object with false inside. The object is truthy.
new Number(0)truthyThis makes an object with 0 inside. The object is truthy.
new String("")truthyThis makes an object with an empty string inside. The object is truthy.
"abc".indexOf("a")falsy"a" is at position 0, so indexOf gives 0, which is falsy. This is a common indexOf bug.
"".lengthfalsyThe empty string has 0 characters, and 0 is falsy.
Number("")falsyConverting an empty string to a number gives 0, which is falsy.
Number("abc")falsy"abc" is not a number, so Number gives NaN, which is falsy.
parseInt("px")falsyparseInt reads digits from the start of the text. "px" has none, so it gives NaN, which is falsy.
1e-400falsyThis number is too tiny for JavaScript to store, so it becomes 0, which is falsy.
0 * -1falsyThe calculation creates -0, and -0 is falsy.
[].lengthfalsyAn empty array is truthy, but its length is the falsy number 0.
" ".trim()falsytrim() removes spaces from both ends. Only spaces were there, so it gives the empty string, which is falsy.
[1, 2].find((n) => n > 5)falsyNo number is bigger than 5, so find finds nothing and gives undefined, which is falsy.
5 - 5falsy5 - 5 is 0, and 0 is falsy.
0.1 + 0.2 - 0.3truthyComputers store 0.1 and 0.2 a tiny bit off. So the result is a tiny number, not exactly 0, and it is truthy.

Booleans in conditions and logic

An if uses truthiness to decide whether to run its block. The operators work like this:

  • ! (not) always gives a real boolean: true or false.
  • || (or) and && (and) are different. They don't always give a boolean. They give back one of the two values you gave them.

This is only a first look. The Logical operators lesson covers them in full.

&& and || give back one of their valuesPop out in the code editor (opens in a new tab)JavaScript
const name = "";const fallback = name || "friend";const subscribed = true;const access = subscribed && "member area";console.log(fallback);console.log(access);console.log(!subscribed);
  • Line 2: name is the empty string, which is falsy. So || gives back the other value, "friend".
  • Line 4: subscribed is true, so && gives back the second value, the string "member area". It does not give the boolean true.
  • Line 7 uses !, so it prints a real boolean: false.

Two traps: Boolean objects and == true

TRAPS

Boolean(x) without new gives a plain boolean. new Boolean(false) is different. It makes an object with false inside it. An object like this is called a wrapper object. Every object is truthy, so this object is truthy too. That makes it dangerous in an if.

A Boolean object is not a plain booleanPop out in the code editor (opens in a new tab)JavaScript
const boxed = new Boolean(false);console.log(Boolean(boxed));console.log(typeof boxed);console.log(boxed.valueOf());console.log(Boolean(false));
  • Line 1 makes the wrapper object and stores it in boxed.
  • Line 2 prints true, because the object is truthy.
  • Line 3 prints "object", the type of boxed.
  • Line 4 uses valueOf(), a method that takes out the value inside. It prints false.
  • Line 5 converts the plain value false, so it prints false.
Left: the plain value false is falsy. Right: new Boolean(false) is an object with false inside it. Every object is truthy, so the object is truthy.falsefalsefalsynew Boolean(false)falseobjecttruthy
On the left is the plain value false. On the right, new Boolean(false) makes an object and puts false inside it. JavaScript checks the object, not what is inside, and every object is truthy.
Don't use new Boolean in your code

When you need a boolean, write true, false, Boolean(value), or a comparison. Don't write new Boolean(...). The same goes for new Number(...) and new String(...): they make objects, not plain values.

The second trap is writing == true or == false. The == operator is called loose equality: it changes the values before it compares them. When one side is a boolean, it turns that boolean into a number: true becomes 1 and false becomes 0. Then it turns the other side into a number too. That gives surprising results.

== true does not check truthinessPop out in the code editor (opens in a new tab)JavaScript
console.log("1" == true);console.log("true" == true);console.log([] == false);console.log(Boolean([]));
  • Line 1: "1" becomes 1 and true becomes 1, so it prints true.
  • Line 2: "true" is not a number, so it becomes NaN. NaN is not equal to 1, so it prints false.
  • Line 3: [] becomes the empty string, then 0. false also becomes 0, so it prints true.
  • Line 4 shows that [] is still truthy. So == false and "is it falsy?" are different questions.

The table asks three questions about each value. Read each row from left to right.

Truthiness is not the same as == true
The valueTruthy? Boolean(value)value == truevalue == false
"1"truetruefalse
"true"truefalsefalse
[]truefalsetrue
0falsefalsetrue
"false"truefalsefalse

Write if (value) when you mean "is it truthy?". Write value === true when it must be the boolean true and nothing else. The === operator is strict equality: it never changes the values before it compares them.

Booleans in real code

Real code uses booleans in many places. Each place asks a different yes/no question.

Name boolean variables like questions: isOpen, hasItems, and isLoggedIn. Then if (isOpen) reads like plain English: "if it is open". Each variable should answer one clear question.

  • Toggles, which switch between on and off: flip a boolean with isOpen = !isOpen.
  • Form controls: a checkbox has a .checked property. It is true when the box is ticked.
  • Arrays: if (items.length) asks whether the list has any items. A length of 0 is falsy.
  • Cleanup: array.filter(Boolean) keeps only the truthy values. It also removes 0 and false.
  • Text from web addresses or forms: it arrives as the string "true" or "false". Compare the exact text.
A toggle, a checkbox, a cart, and a userPop out in the code editor (opens in a new tab)JavaScript
let isOpen = false;isOpen = !isOpen;console.log(isOpen);const checkbox = { checked: true };console.log(checkbox.checked);const cart = [];const hasItems = cart.length > 0;const user = { name: "Asha" };const isLoggedIn = Boolean(user);console.log(hasItems, isLoggedIn);
  • Line 1: the toggle starts as false, which means closed.
  • Line 2: !isOpen gives the opposite, so isOpen becomes true. Line 3 prints true.
  • Line 5 prints the checkbox's checked property, true.
  • Line 7: the cart is empty, so its length is 0 and hasItems is false.
  • Line 9: user is an object, so Boolean(user) is true.
  • Line 10 prints false true.
Check for items, then remove falsy valuesPop out in the code editor (opens in a new tab)JavaScript
const items = ["hat", "coat"];if (items.length) {  console.log("show cart");}const mixed = [0, 1, "", "Ada", null, false, true];console.log(mixed.filter(Boolean).join("|"));
  • Line 2: the list has 2 items. 2 is truthy, so line 3 prints show cart.
  • Line 6: filter(Boolean) keeps only the truthy values. It removes 0, "", null, and false. Then join("|") puts the kept values into one string with | between them, so it prints 1|Ada|true.
Read "true" and "false" text safelyPop out in the code editor (opens in a new tab)JavaScript
const fromUrl = "false";console.log(Boolean(fromUrl));console.log(fromUrl === "true");console.log(fromUrl === "false" ? false : fromUrl === "true");
  • Line 1: text from a web address is always a string. Here it is "false".
  • Line 2 prints true, because the string is not empty. So Boolean() can't read this text for you.
  • Line 3 compares the exact text and prints false.
  • Line 4 uses ? :, a short if/else. If the text is "false", the answer is false. If not, the answer is whether the text is "true". Here it prints false.

Common misconceptions

  • "The string "false" is false." It is a string with characters in it, so it is truthy.
  • "A string of spaces is empty." " " and "\n" (a new line) contain characters, so they are truthy.
  • "Empty arrays and objects are falsy." They are objects, and objects are truthy.
  • "new Boolean(false) works like false." It is an object, so it is truthy.
  • "== true checks truthiness." It turns both sides into numbers first, so it can give surprising answers.
  • "filter(Boolean) only removes missing values." It removes every falsy value, including 0 and false.
  • "if (value) only checks for a missing value." It also skips false, 0, "", and NaN.

Practice exercises

6 EXERCISES
Exercise 1 · Warm-upPredict booleans from questions

What do the four lines print? Write them in order.

Starter codePop out in the code editor (opens in a new tab)JavaScript
console.log(typeof true);
console.log(5 > 3);
console.log("a" === "A");
console.log(!"hello".includes("z"));

Answer, then press Check. Spacing and letter case don’t matter.

    Exercise 2 · Warm-upCount the falsy values

    How many values are on the falsy list?

    Starter codePop out in the code editor (opens in a new tab)JavaScript
    const falsy = [false, 0, -0, 0n, "", null, undefined, NaN];
    console.log(falsy.length);
    console.log(falsy.every((value) => !value));

    Answer, then press Check. Spacing and letter case don’t matter.

      Exercise 3 · PracticeRead true/false text from a web address

      A web address gave you the string "false". What does the exact comparison on line 3 print?

      Starter codePop out in the code editor (opens in a new tab)JavaScript
      const raw = "false";
      console.log(Boolean(raw));
      console.log(raw === "true");

      Answer, then press Check. Spacing and letter case don’t matter.

        Exercise 4 · PracticeWhat does filter(Boolean) keep?

        What does line 2 print?

        Starter codePop out in the code editor (opens in a new tab)JavaScript
        const values = [0, 1, 2, "", "ok", null, false, true];
        console.log(values.filter(Boolean).join("|"));

        Answer, then press Check. Spacing and letter case don’t matter.

          Exercise 5 · PracticeFind the indexOf bug

          The code prints missed, but "a" is in the string. Why?

          Starter codePop out in the code editor (opens in a new tab)JavaScript
          const position = "abc".indexOf("a");
          if (position) {
            console.log("found");
          } else {
            console.log("missed");
          }

          Answer, then press Check. Spacing and letter case don’t matter.

            Exercise 6 · ChallengeSpot the Boolean object

            What do the three lines print?

            Starter codePop out in the code editor (opens in a new tab)JavaScript
            const boxed = new Boolean(false);
            console.log(Boolean(boxed));
            console.log(typeof boxed);
            console.log(Boolean(false));

            Answer, then press Check. Spacing and letter case don’t matter.

              Check your understanding

              8 QUESTIONS
              Booleans & truthiness quiz · 8 questionsScore: first tries count
              1. Question 1 of 8How many boolean values does JavaScript have?

                Choose an answer to see the explanation.

              2. Question 2 of 8What do the comparison and method print?

                Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
                console.log(5 > 3);
                console.log("hello".includes("z"));

                Choose an answer to see the explanation.

              3. Question 3 of 8What does this print?

                Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
                console.log(Boolean("false"));

                Choose an answer to see the explanation.

              4. Question 4 of 8What do the empty and space strings print?

                Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
                console.log(Boolean(""));
                console.log(Boolean(" "));

                Choose an answer to see the explanation.

              5. Question 5 of 8What does this array check print?

                Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
                console.log(Boolean([]));
                console.log([].length === 0);

                Choose an answer to see the explanation.

              6. Question 6 of 8What do these loose equality checks print?

                Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
                console.log("1" == true);
                console.log("true" == true);

                Choose an answer to see the explanation.

              7. Question 7 of 8What does a Boolean object do?

                Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
                const value = new Boolean(false);
                console.log(Boolean(value));
                console.log(typeof value);

                Choose an answer to see the explanation.

              8. Question 8 of 8What does this tiny number print?

                Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
                console.log(Boolean(0.1 + 0.2 - 0.3));

                Choose an answer to see the explanation.

              Key takeaways

              • There are exactly two booleans: true and false.
              • Comparisons, yes/no methods, and ! give booleans.
              • The eight falsy values are false, 0, -0, 0n, "", null, undefined, and NaN.
              • Every other value is truthy. That includes "false", [], {}, and new Boolean(false).
              • Don't write == true. Use if (value) to check truthiness, or value === true to check for the boolean true.
              • In real code, pick the check that fits the question: items.length for "any items?", filter(Boolean) to drop falsy values, and raw === "true" to read text.

              Final definition: a boolean is the yes/no value true or false. Truthiness is how JavaScript reads any other value as true or false when it needs a yes/no answer.

              Up next: undefined, the value JavaScript uses when nothing has been given yet.

              CompleteFrontend Clear concepts. Working examples.