Codiqo

Learn JavaScript · lesson 11 of 60 · about 7 minutes

Booleans and Logical Operators

Decide who can comment on Sam's page: compare values, and combine yes-or-no answers with &&, || and !.

What you'll learn

  • Compare with < > <= >=
  • Combine with && and ||
  • Flip with !

true and false

Comparisons like <, >, <= and >= give a boolean: true or false. Booleans are what decisions are made of: is this post long, is this visitor signed in, may they comment?

Comparisons

const likes = 13;
console.log(likes >= 10);
console.log(likes < 5);

Output

true
false

Combining answers

  • && means and: true only when both sides are true
  • || means or: true when at least one side is true
  • ! means not: it flips true and false

&& is worked out before ||, like * before +. Brackets make the order clear.

Who can comment?

const signedIn = true;
const muted = false;
console.log(signedIn && muted);
console.log(signedIn || muted);
console.log(!muted);
console.log(signedIn && !muted);

Output

false
true
true
true

Tip

JavaScript has no and or or words: it's && and ||.

Now try it yourself

This lesson is free in Codiqo, with 10 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.