Camp 1 · Step 3 of 18
Values, expressions, and errors
How JavaScript evaluates expressions, and how to read its error messages without flinching.
Programs are built from expressions — pieces of code that produce a
value. 2 + 2 is an expression. So is "Kode" + "Trail". Understanding
"this code becomes a value" is a quiet superpower.
Expressions evaluate
JavaScript works from the inside out: (2000 + 500) becomes 2500, then
glues onto the string. Notice + does double duty — addition for
numbers, concatenation for strings. That leads to JavaScript's most
famous quirk:
"2" + 2 gives "22" — when one side is a string, JavaScript converts the
other side and concatenates. Keep quotes for text and bare digits for math,
and this will never bite you.
Errors are guides
Run this broken line:
ReferenceError: greeting is not defined — JavaScript's version of "I've never heard of this name." The error type tells you the category, the message names the culprit. Compare:
| Error | Usually means |
|---|---|
ReferenceError: x is not defined | typo, or using a name before creating it |
SyntaxError: unexpected token | JavaScript couldn't parse it — often a missing ) or quote |
TypeError: x is not a function | calling something that isn't callable — often a typo like console.lg |
What does console.log("1" + 1) print?
You see: TypeError: console.lgo is not a function. What happened?