Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Type Casting  ›  Capstone Project

Implicit Type Casting

Implicit Type Casting 12 minWhen operators meet mixed types, + joins as text if either side is a string, other arithmetic operators convert both sides to numbers, and conditions convert to booleans.

Implicit Type Casting

In short: when mixed types meet an operator, + is the special case: if either side is a string, both sides become strings and get joined. Every other arithmetic operator (-, *, /, %) only understands numbers, so it coerces both sides to numbers first. Conditions coerce to boolean using the falsy-eight rule. Evaluate left to right, one operator at a time, and ask at each +: "is either side a string right now?" — not what it started as.

A conveyor belt evaluating 1 + 2 + "3" + 4 left to right: 1 + 2 adds to 3 (no string yet), then 3 + "3" joins to "33" once a string appears, then "33" + 4 joins again to "334" because it's already a string

A fade-out that becomes text

A length field holds a string; adding seconds the careless way does not add at all:

javascript

"214" + 6 joins to "2146" because a string is on the left. 6 + "214" joins too — + doesn't care which side the string is on. 214 + 6 has no string at all, so it adds normally to 220. true + 1 also has no string, so true coerces to the number 1 instead, giving 2.

The other operators never join

Subtract, multiply and divide only ever mean "make this a number":

javascript

"214" - 6 is 208: minus has no joining behaviour, so it coerces the string to a number the same way Number() would. "abc" - 1 can't become a number at all, so the result is NaN. null + 1 is 1 — null coerces to 0 for arithmetic, and since neither side is a string, + adds rather than joins.

Reading a chain of operators left to right

Order changes everything, because once a string appears, every + after it joins too:

javascript

1 + 2 + "3" + 4 - 1 goes: 1 + 2 is 3 (no string yet), 3 + "3" joins to "33", "33" + 4 joins to "334", then "334" - 1 finally needs a number and gives 333. But "1" + 2 + 3 starts with the string already in place: "1" + 2 is "12", then "12" + 3 is "123" — the same digits, completely different answer, because of where the string sat.

Evaluating 1 + 2 + "3" + 4 - 1Step 1 of 4
  1. First pair: 1 + 2

    No strings. Add numbers: 3.

The safe habit: convert form input with Number() the moment you read it, so no later + can ever meet a string by accident.

Quick practice

quick drill

What is "5" + 2?

quick drill

What is "5" - 2?

quick drill

What is 1 + 2 + "3"?

quick drill

What is true + true?

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainTrace the expression

Evaluate "3" * 2 + 1 + "0" step by step and explain each coercion. Then explain how to make a form's total always add as numbers.