Skip to main content
Module 1 · Lesson 3 of 43/32 in the course~16 min
Module lessons (3/4)

Operators

Operators take one or more input values and return a new one without modifying the originals. Knowing precedence and short-circuiting rules avoids subtle errors in fallback conditions and expressions.

Mental model: strict equality and short circuit

FamilyExamplesResultGolden rule
Arithmetics17 % 5, 2 ** 102, 1024Use ( ) when combining + with * or **
Close comparison3 === '3', 3 !== '3'false, trueNever use == or != in new code
Logicala && b, a || b, !aValue of the decisive operandThey stop as soon as the outcome is certain
Coalescencea ?? bb only if a is null/undefinedRespect 0, false and ''

Progressive examples

1. Modulo %, power ** and explicit brackets

JS
17 % 5; // 2 (resto utile per testare pari/dispari: n % 2 === 0)
2 ** 10; // 1024
2 + 3 * 4; // 14 (prodotto prima della somma)
(2 + 3) * 4; // 20 (parentesi esplicite)

2. Why === always beats ==

JS
0 == ''; // true (trappola di coercion implicita!)
0 === ''; // false (tipi diversi: number vs string)
'7' !== 7; // true

3. Return value of &&, || and ??

JS
const rawName = '';
const withOr = rawName || 'Anonymous'; // 'Anonymous' ('' è falsy)
const count = 0;
const badRetries = count || 3; // 3 (bug se 0 è un numero valido!)
const safeRetries = count ?? 3; // 0 (?? scatta solo per null/undefined)

4. Compare scope and evaluation order

JS
const isAdmin = true;
const canEdit = isAdmin && 'write-access'; // 'write-access'

Guided laboratory

Practice Stage

Arithmetic precedence

Exercise#js.m1.l3.e1
Attempts: 0Loading…

Calculates (8 + 4) raised to the power of 2 and divided by 6. The last expression must evaluate to exactly 24.

Loading editor…
Show hint

Put (8 + 4) in parentheses before the ** operator.

Solution available after 3 attempts

Practice Stage

Close comparison combined with AND

Exercise#js.m1.l3.e2
Attempts: 0Loading…

Write a Boolean expression that combines && with a close comparison of 7 === 7 and 5 < 10, returning true.

Loading editor…
Show hint

Write 7 === 7 && 5 < 10;.

Solution available after 3 attempts

Practice Stage

Default safe for 0 with ??

Exercise#js.m1.l3.e3
Attempts: 0Loading…

Implement normalizeRetries(value) that returns value if it is a valid number other than null/undefined (including 0), otherwise returns 3. Avoid the || trap!

Loading editor…
Show hint

With value ?? 3, both 0 and other numbers remain intact.

Solution available after 3 attempts

Check for understanding

JS Quiz#js.m1.l3.e4
Ready

What is the result of 0 || 5 compared to 0 ?? 5?

Answer options
JS Quiz#js.m1.l3.e5
Ready

What value is returned by 'admin' && 42 in JavaScript?

Answer options