← Back to course: JavaScript from scratch

Arithmetic, assignment and logical operators

Video coming soon

An operator is a symbol that combines values to produce another value. You have already used the plus sign and the equals sign; in this lesson you will see the full set of operators you need day to day. Mastering them is what lets you move from simply storing data to calculating with it and making decisions based on it.

Arithmetic operators

Addition (+), subtraction (-), multiplication (*) and division (/) work as in mathematics and respect precedence: multiplication and division first, then addition and subtraction; parentheses change the order. You also have the remainder or modulo (%), which returns what is left over from a division and is useful to know whether a number is even; and exponentiation (**), which raises a number to a power. The + operator also joins texts, something we will see in the next lesson.

javascript
console.log(7 + 3 * 2);   // 13 (multiplication first)
console.log((7 + 3) * 2); // 20
console.log(10 / 4);      // 2.5
console.log(10 % 3);      // 1
console.log(2 ** 10);     // 1024
console.log(8 % 2 === 0); // true -> 8 is even

let counter = 5;
counter++;   // 6
counter--;   // 5
counter += 10; // 15
counter *= 2;  // 30
console.log(counter);

Notice the assignment shortcuts: += adds and assigns, -= subtracts and assigns, *= multiplies and assigns, and so on. The ++ and -- operators add or subtract one. They are very handy in counters and loops, and you will use them constantly from week 3.

Comparison and logical operators

Comparison operators (>, <, >=, <=, ===, !==) return a boolean. Logical operators combine booleans: && (and) is true only if both sides are, || (or) is true if at least one is, and ! (not) flips the value. There is an important peculiarity: && and || do not necessarily return true or false, but one of the two operands, and they evaluate in short circuit, meaning they stop as soon as they know the result.

javascript
const age = 20;
const hasTicket = true;

console.log(age >= 18 && hasTicket); // true
console.log(age < 18 || hasTicket);  // true
console.log(!hasTicket);             // false

// Short circuit: default values
const userName = '' || 'Guest';
console.log(userName); // Guest

// ?? only replaces null or undefined
const count = 0 ?? 10;
console.log(count); // 0
console.log(0 || 10); // 10

|| versus ??

The || operator replaces any falsy value (0, empty string, false, null, undefined, NaN), while ?? (nullish coalescing) only replaces null and undefined. If 0 is a legitimate value, such as an amount, use ??. It is a classic mistake to write amount || 1 and see a valid 0 turned into 1.

A practical case

Imagine a shop that applies a 15% discount to purchases over 50 euros and free shipping from 100. With arithmetic operators you compute the discount, with comparison operators you decide whether it applies and with && or || you combine the conditions. Thinking this way, breaking the rule into small pieces, is the main skill of programming. Before writing code, note the rule in your own words and then translate it operator by operator; if the result surprises you, print the intermediate values.

✎ My notes

Saved only in this browser. Sign in to keep it in your account.0/5000 characters

← PreviousNext →