LESSON 17 OF 50

Invert a boolean value

Use ! to negate a condition.

Official sourceECMAScript — logical NOT operatorReviewed: 2026-08-26
STEP 1 · LEARN

How it works

The logical NOT operator converts its operand to boolean and reverses the result. Negating false produces true.

Correct example
const isOpen = false;
const isClosed = !isOpen;
console.log(isClosed);

isClosed receives true without manually repeating the value.

Common mistake
const isClosed = isOpen;

Copying the value preserves false; it does not invert it.

STEP 2 · APPLY

Your exercise

Declare isClosed as the negation of isOpen and print the result.

STEP 3 · RUN

Write and run the code

script.jsJAVASCRIPT
Isolated consoleisolated
Preparing the runtime…
Run the code to see the result.
The draft stays only in this browser.Isolated QuickJS · no DOM, session or network
STEP 4 · CHECK

What needs to pass

0%not checked
  • isOpen and isClosed are declared with const.
  • isClosed uses !isOpen.
  • The output is true.
Explain the solution logic

The ! operator calculates the opposite value, and the console confirms the resulting boolean.