Search⌘ K
AI Features

Logical Operators: !, ||, &&

Explore how to use JavaScript logical operators NOT, OR, and AND within if-statements. Understand their rules and precedence to create complex logical conditions that control code flow effectively.

Let’s introduce some logical operators to make if-statements more powerful.

!

The ! operator is called the “not”-operator. It does two things to a value: it coerces it to either true or false, and then gives the opposite value. Falsey values become true and truthy values become false.

For example, !true will become false. !0 will become true. !'' will become true. Think of this operator as saying “This item is NOT true” or “This item is NOT false”.

Node.js
// Falsey values become true
console.log(!false); // -> true
console.log(!''); // -> true
console.log(!0); // -> true
console.log(!null); // -> true
console.log(!undefined); // -> true
console.log(!NaN); // -> true
// Truthy values become false
console.log(!true); // -> false
console.log(!1); // -> false
console.log(!'abc'); // -> false
console.log(!9999999); // -> false
console.log(!'ha*UIHJ'); // -> false

! in if-statements

We can use this in ...