Find a course

Module 2: Operators and DecisionsLesson 3 of 1212 min

Operators, Expressions and Type Coercion

Use arithmetic, comparison and assignment operators, understand how JavaScript converts types automatically, and always compare safely with ===.

Step 1 of 14Learn

When 1 + 1 equals 11

A developer builds a form that adds two numbers. A user types 1 and 1 and clicks Add. The page proudly displays 11. What went wrong? Values from form fields are strings, and for strings, + means join, not add: "1" + "1" is "11".

Operators are the symbols that combine values into expressions. The arithmetic ones:

OperatorMeaningExampleResult
+add (or join strings)7 + 29
-subtract7 - 25
*multiply7 * 214
/divide7 / 23.5
%remainder7 % 21
**power7 ** 249

The remainder operator % is more useful than it looks. n % 2 === 0 tests whether n is even. minutes % 60 gives the leftover minutes after whole hours.

Precedence follows math rules: *, / and % before + and -. So 2 + 3 * 4 is 14. Use parentheses whenever the order could be unclear to a reader: (2 + 3) * 4 is 20.

Assignment shortcuts update a variable in place: x += 5 means x = x + 5; x *= 2 doubles it; count++ adds 1.

Step 2 of 14Try the calculator

Operator playground

Change the two numbers and watch every arithmetic operator at once. Try 7 and 2, then 10 and 3, then a negative number.

a + b
22
a - b
12
a * b
85
a / b
3.4
a % b (remainder, for positive a)
2
a ** b
1,419,857

Step 3 of 14Practice · Work it out

What is the value of 17 % 5 + 2 ** 3?

Step 4 of 14Learn

Type coercion: JavaScript guesses

When an operator gets values of different types, JavaScript coerces (converts) one of them. The rules are consistent, but not always intuitive:

  • + with any string joins: "5" + 3 → "53", 3 + 4 + "px" → "7px" (left to right: 3 + 4 first).
  • Other arithmetic converts to numbers: "5" - 3 → 2, "6" * "2" → 12, "abc" - 1 → NaN.
  • Booleans become numbers in math: true + 1 → 2.

The fix is to convert explicitly before calculating:

const a = Number(input1.value);
const b = Number(input2.value);
console.log(a + b); // real addition

Explicit conversion makes your intent obvious and prevents the "11" bug. Common converters: Number(x), String(x), Boolean(x), parseInt(x) and parseFloat(x).

A related tool: Number.isNaN(x) checks whether a conversion failed. You cannot test with x === NaN, because NaN is the only value in JavaScript that is not equal to itself.

Step 5 of 14Practice · Match the pairs

Match each expression to its result.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. "5" + 3
  2. "5" - 3
  3. "5" * "2"
  4. 1 + 2 + "3"
  5. "1" + 2 + 3

Step 6 of 14Learn

Comparing values: always use ===

Comparison operators produce booleans: >, <, >=, <=, and two kinds of equality.

  • Strict equality === checks that the value and the type match. 5 === 5 is true; 5 === "5" is false.
  • Loose equality == converts types first, then compares. 5 == "5" is true, 0 == "" is true, null == undefined is true, and 0 == "0" is true while "" == "0" is false.

Those loose rules produce bugs that are hard to spot. The near-universal advice: use === and !==, and convert types yourself when you need to.

Comparing strings works alphabetically by character code: "apple" < "banana" is true. But uppercase letters come before lowercase, so "Zebra" < "apple" is also true. And numbers stored as strings compare character by character: "10" < "9" is true, because "1" comes before "9". Convert to numbers before comparing numeric input.

Remember that = is not a comparison at all. A single = assigns. Writing if (x = 5) sets x to 5 instead of testing it, a classic typo that linters catch.

Step 7 of 14Compare

Strict === versus loose ==

Aspect=== (strict)== (loose)
5 vs "5"falsetrue
0 vs ""falsetrue
null vs undefinedfalsetrue
0 vs "0"falsetrue
"" vs "0"falsefalse (!)
NaN vs NaNfalsefalse

Step 8 of 14Note

Step 9 of 14Compare

Fallbacks: || versus ??

Aspectx || 1x ?? 1
x = 01 (0 is falsy)0 (kept)
x = ""1"" (kept)
x = false1false (kept)
x = null11
x = undefined11
x = 555

Step 10 of 14Practice · Sort into groups

Does each expression evaluate to true or false?

Tap an item, then tap its group (or drag it there).

Step 11 of 14Note

Step 12 of 14Practice · Code challenge

Write a function addInputs(a, b) that receives two strings containing numbers (like form values) and returns their numeric sum.

Runs in a sandbox in your browser.
  • addInputs("1", "1")
  • addInputs("2.5", "4")
  • addInputs("10", "-3")

Step 13 of 14Tap to reveal

Why JavaScript converts types at all

  1. A language built to forgive

    JavaScript was designed in 1995 for small scripts written by people who were not professional programmers. Its designers chose to keep going when types did not match instead of stopping with an error, so a page would still work if a number arrived as text. That decision is why "5" - 3 quietly gives 2.

  2. The cost of forgiveness

    Silent conversion hides mistakes. A form value that should be a number stays a string, + joins instead of adding, and the wrong total appears on screen with no error anywhere. The bug shows up far from its cause, which makes it slow to find.

  3. The modern answer

    Professional JavaScript developers convert values explicitly with Number(), String() and Boolean(), compare with === and !==, and use tools that flag risky patterns. Many teams also use TypeScript, a version of JavaScript with type annotations that catches these mistakes before the code runs.

  4. A rule you can use today

    When a value comes from outside your program, such as a form field, a URL or a file, assume it is a string and convert it on purpose before doing math or comparing it to numbers.

Step 14 of 14Practice · Choose one

What does console.log(10 + "5" - 2) print?