Home › Learn › JavaScript › Making decisions

Comparisons and logic

Lesson 6 of 40. Ask questions about values and combine the answers.

Comparisons give true or false: === equal, !== not equal, <, >, <=, >=. Always use the triple forms; == converts types before comparing ("1" == 1 is true) and causes bugs. && is and, || is or, ! is not.

A boolean can be stored in a variable, which keeps conditions readable: name the question, then use the name. Unlike some languages, JavaScript has no chained comparison, so "between 13 and 19" is age >= 13 && age <= 19.

const temperature = 31;
const isHot = temperature > 28;
const isWeekend = false;
console.log(isHot && !isWeekend);   // true

Your task

Using age and hasId from the starter, make isAdult (18 or older), canEnter (an adult who has an id), and isTeen (13 to 19 inclusive). Compute them; do not type true or false.

Hint 1

const isAdult = age >= 18;

Hint 2

const isTeen = age >= 13 && age <= 19;

Show the solution
const age = 20;
const hasId = true;

const isAdult = age >= 18;
const canEnter = isAdult && hasId;
const isTeen = age >= 13 && age <= 19;

    Runs in an isolated web worker in this tab; nothing is sent anywhere.