In this lesson you will
- Use arithmetic operators, including % for remainders and ** for powers
- Compare values safely with === and !== and explain what == does wrong
- Write if, else if and else chains and short ternary expressions
- Predict which values count as true or false, and choose between || and ??
Arithmetic and assignment operators
JavaScript does maths with +, -, * and /, in the order you learnt at school: multiplication and division before addition and subtraction, and brackets first. % gives the remainder of a division, so 17 % 5 is 2; it answers questions such as "is this number even?" (n % 2 === 0) or "how many fils are left over?". ** raises to a power: 1.05 ** 3 is the growth of a price after three years of 5% increases.
Assignment operators update a let variable in place. total += price means total = total + price, and -=, *= and /= work the same way. count++ adds one and count-- subtracts one. These short forms appear in almost every loop and running total, so it pays to read them fluently.
Console
- Console messages, errors, form data and clicked links appear here.
Math.floor() rounds down, and % gives what remains. The last line prints 14 and 20: brackets change the order.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
What is the difference between == and === in JavaScript?
=== checks that the value and the type are both the same, with no conversion. == converts the two values to a common type first, so "1" == 1 and 0 == "" are true. Use === and !== in your code; most style guides and linters require it.
What are truthy and falsy values?
When a value is used as a condition, JavaScript treats false, 0, -0, 0n, "", null, undefined and NaN as false. These are falsy. Every other value, including "0", "false", [] and {}, is truthy.
When should I use the ternary operator?
Use condition ? a : b when you choose between two simple values, such as a fee or a label. If either side needs several statements, or you are tempted to nest another ternary, write an if / else instead.
What does the ?? operator do?
It is the nullish coalescing operator. a ?? b gives a unless a is null or undefined, in which case it gives b. Unlike ||, it keeps valid values such as 0 and the empty string.
When is switch better than if else in JavaScript?
When you compare one value against several fixed options, such as a country code or a menu choice. Each case uses strict comparison, and you end it with break, or the code falls through to the next case. For ranges such as "over 100", an if chain is clearer.
Sources
Facts in this lesson were checked against these official pages on 5 November 2026.
- MDN: Making decisions in your code (conditionals)
- MDN: Expressions and operators
- MDN: Strict equality (===)
- MDN: Falsy
- MDN: Nullish coalescing operator (??)
- MDN: Conditional (ternary) operator
- MDN: switch
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.