JavaScript Tutorial

JavaScript Booleans

true and false. Comparisons produce them. Boolean(value) and if (value) show what is truthy.

Two literals

A boolean is true or false. Write them in lowercase. True andFalse are Python; in JavaScript they are ordinary names, not the boolean values.

You store a yes-or-no fact: a box is checked, a score is high enough, a list still has items. Conditions consume booleans. Comparisons produce them.

Run the examples in /javascript/try. The console prints the wordstrue and false.

Comparisons produce booleans

7 > 3 is true. 7 === 3 is false. You can store that result, log it, or pass it into if in the next chapter. The comparison operators are covered in full under Comparisons.

Example

const ready = true;
const empty = false;
console.log(ready);
console.log(empty);
console.log(7 > 3);
console.log(7 === 3);
console.log(typeof ready);

document.body.textContent =
  String(ready) + " " + String(7 > 3) + " " + typeof ready;

Boolean(value)

Boolean(value) converts any value to true or false using the same rule if uses. It does not change the original value. Call it when you want to seehow a value will behave as a condition.

Example

console.log(Boolean(1));
console.log(Boolean(0));
console.log(Boolean("hi"));
console.log(Boolean(""));
console.log(Boolean(null));
console.log(Boolean([]));

document.body.textContent =
  String(Boolean("hi")) + " " + String(Boolean("")) + " " + String(Boolean(0));

Truthy and falsy

Values that become false in a boolean context are called falsy. Everything else is truthy. Memorize the falsy list. It is short.

Falsy valueNotes
falseThe boolean itself
0 and -0The number zero
0nBigint zero
""Empty string
nullIntentionally empty
undefinedNo value assigned
NaNNot a number

"0" is a non-empty string, so it is truthy. [] and {} are objects, so they are truthy even when they hold nothing.

if (value) uses the same rule

You do not have to write if (Boolean(value) === true). if (value) already converts. An empty string skips the block. A non-empty string runs it. 0 skips. Any other number runs.

Example

function label(value) {
  if (value) {
    return "truthy";
  }
  return "falsy";
}

console.log(label("hello"));
console.log(label(""));
console.log(label(0));
console.log(label(3));
console.log(label(undefined));

document.body.textContent =
  label("hello") + " / " + label("") + " / " + label(0);

Not, and double not

!value flips a boolean: !true is false. Applied to a non-boolean, it converts first, then flips: !"" is true because the empty string is falsy.

!!value converts to boolean twice and lands on the same result as Boolean(value). You will see it in other people’s code. Boolean(value) is easier to read.

Prefer a real comparison when the value is a number you care about: if (count > 0) is clearer than if (count), because 0 is a valid count, not an error.

What to remember

  • Literals are true and false in lowercase.
  • Comparisons produce booleans.
  • The falsy list is short. Empty array and empty object are truthy.
  • Boolean(value) and if (value) follow that same list.

Next: comparisons — === versus ==, and why the extra equals sign matters.

FAQ: JavaScript Booleans

Common questions about this page.

What is the StudyGrid JavaScript tutorial?

The StudyGrid JavaScript tutorial is a full beginner track: variables, functions, arrays, if/else, the DOM, events, storage, and fetch. Each chapter has copy-and-run examples.

Should I run javascript booleans examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn javascript booleans in this JavaScript JavaScript lesson (JavaScript Booleans).

Is the JavaScript editor the same as Try Python?

No. Try JavaScript is a live page plus a console at /javascript/try. Try Python stays at /try. JavaScript lessons never open the Python editor.

Do I need to install anything to learn JavaScript?

No. Open a chapter, click Try it in JavaScript, and the page and console update in the browser.

Where should I start the JavaScript tutorial?

Start at JavaScript Intro, then Where To, Output, and Syntax. After variables and functions, continue to the DOM and events. Use Next at the bottom of each chapter.

Is the JavaScript tutorial free?

Yes. The JavaScript studio on StudyGrid (studygrid.in) is free: dashboard, chapters, and the live editor.