Combine boolean checks with && and ||.

logical-and-or `&&` requires both sides to be true. `||` allows either side to be true.

Logical And Or

age
logical_and_or.js
Replay: real traced execution (multi-file project)
const age = 20;
const hasPass = true;
const canEnter = age >= 18 && hasPass;
const discount = age < 18 || age >= 65;

console.log("age=" + age);
console.log("canEnter=" + canEnter);
console.log("discount=" + discount);
const age = 16;
const hasPass = true;
const canEnter = age >= 18 && hasPass;
const discount = age < 18 || age >= 65;

console.log("age=" + age);
console.log("canEnter=" + canEnter);
console.log("discount=" + discount);
const age = 70;
const hasPass = true;
const canEnter = age >= 18 && hasPass;
const discount = age < 18 || age >= 65;

console.log("age=" + age);
console.log("canEnter=" + canEnter);
console.log("discount=" + discount);
  1. age ← 20, hasPass ← true, canEnter ← true, discount ← false

    1const age→ 20 = 20; //@age=16, 702const hasPass→ true = true;3const canEnter→ true = age→ 20 >= 18 && hasPasstrue;4const discount→ false = age→ 20 < 18 || age >= 65;56console.log("age=" + age20);7console.log("canEnter=" + canEntertrue);8console.log("discount=" + discountfalse);
    outputage=20
    canEnter=true
    discount=false
  1. age ← 16, hasPass ← true, canEnter ← false, discount ← true

    1const age→ 16 = 16;2const hasPass→ true = true;3const canEnter→ false = age→ 16 >= 18 && hasPasstrue;4const discount→ true = age→ 16 < 18 || age >= 65;56console.log("age=" + age16);7console.log("canEnter=" + canEnterfalse);8console.log("discount=" + discounttrue);
    outputage=16
    canEnter=false
    discount=true
  1. age ← 70, hasPass ← true, canEnter ← true, discount ← true

    1const age→ 70 = 70;2const hasPass→ true = true;3const canEnter→ true = age→ 70 >= 18 && hasPasstrue;4const discount→ true = age→ 70 < 18 || age >= 65;56console.log("age=" + age70);7console.log("canEnter=" + canEntertrue);8console.log("discount=" + discounttrue);
    outputage=70
    canEnter=true
    discount=true