Search⌘ K
AI Features

Logical Operators

Explore how logical operators combine statements to return Boolean values in JavaScript. Learn about negation, logical AND, and OR through practical examples including a simplified 'Guess Who?' game. Understand how these operators impact code behavior and how to use them effectively in programming.

Logical operators can be used to combine two or more statements to produce a compound statement that returns a Boolean value.

Guess who?

“Guess who?” is a popular family game that involves trying to guess a person by asking “yes” or “no” questions about their appearance. We’re going to consider a smaller version of the game to demonstrate some logical operators.

Our smaller game only has four characters: Alfie, Betty, Gemma and Del.

The four characters in our "Guess who?" game
The four characters in our "Guess who?" game

Each character has two particular characteristics: they either wear a hat or they don’t, and they either wear glasses or they don’t.

For example, if we asked “Who is wearing glasses?”, the answer would be “Betty and Del”.

Who is wearing glasses?
Who is wearing glasses?

Negation (logical NOT)

Negation returns the opposite of a value’s Boolean value. So truthy values will ...