Orientation: Running and Reading JavaScript
What JavaScript is, where it runs, and how to read the statements, expressions, and errors in a small program.
By the end of this lesson, you can
- Distinguish ECMAScript, the language, from host APIs such as the DOM or Node's file system module
- Run the same short program in a browser console, a browser script file, and Node.js
- Tell statements apart from expressions, and explain what semicolon insertion does for you
- Read a thrown error's type and message to find the line that caused it
Why it matters
“JavaScript” usually means two things bundled together: a language, and a set of extra tools the place running it hands you. Confusing the two is the single most common source of “why does this work in the console but not when I run the file?” bugs. Getting oriented here — what’s the language, what’s the environment, and how to read an error message — pays off in every lesson that follows.
Mental model
The language itself is standardized as ECMAScript. A JavaScript engine (V8 in Chrome and
Node, SpiderMonkey in Firefox, JavaScriptCore in Safari) implements that standard: let, const,
functions, arrays, Promise, and so on all work identically everywhere.
Everything else — console.log, the DOM (document, window), fetch, Node’s fs and
process — is a host API. The browser and Node each hand your script a different toolbox on
top of the same language:
| Layer | Example | Same everywhere? |
|---|---|---|
| ECMAScript (the language) | const, for, Array.prototype.map |
Yes |
| Browser host APIs | document, window, fetch, localStorage |
No — Node doesn’t have these |
| Node host APIs | require/import for files, process, fs |
No — browsers don’t have these |
Small example: three ways to run the same program
The same two-line program, run three different ways, produces the same output every time — because it only uses language features.
let greeting = 'Hello, JavaScript!';
console.log(greeting);Hello, JavaScript!The same file, saved as hello.js and run with node hello.js from a terminal, prints the exact
same line — Node’s engine (V8) executes the same ECMAScript, and console.log happens to exist as
a host API in both places.
Statements and expressions
An expression is anything that produces a value: 5 + 3, "hi".toUpperCase(), greeting. A
statement is a complete instruction — expressions are often part of a statement, but a
statement itself doesn’t produce a value you can use.
| Code | Statement or expression? | Why |
|---|---|---|
| 5 + 3 | Expression | Produces the value 8 — you could write console.log(5 + 3). |
| let x = 5; | Statement | Declares a binding. It contains the expression 5, but the whole line isn't a value. |
| console.log(greeting); | Expression statement | A function-call expression, used on its own as a statement. |
| if (x > 0) { } | Statement | Control flow. There's no if expression in JavaScript. |
Statements are normally separated by semicolons. JavaScript will insert missing ones for you through a process called Automatic Semicolon Insertion (ASI) — which is exactly why the following is a well-known trap:
function makeGreeting() {
return
{
text: 'Hello!',
};
}
console.log(makeGreeting());undefinedReading an error message
Errors are evidence, not failures. Two you’ll see constantly this early on:
console.log(greting);Uncaught ReferenceError: greting is not definedThat’s a ReferenceError — the code is grammatically valid, but at the moment it ran, no
binding named greting existed (note the typo). Compare it with a error the engine catches before
running anything at all:
console.log('Hello';Uncaught SyntaxError: missing ) after argument listA SyntaxError means the text isn’t valid JavaScript at all — nothing in the file runs, not
even lines before the mistake. ReferenceError means the syntax was fine, but something the code
pointed at doesn’t exist. Both messages name the error type first, which is the fastest way to know
which category of problem you’re looking at before reading further.
Check your understanding
Or reveal the answer without checking
Answer:undefined
Automatic Semicolon Insertion places a semicolon immediately after return because a line break follows it, so the function returns undefined and 1 + 2; never executes as part of the return value.
Or reveal the answer without checking
Answer:The whole file failed to parse before any of it executed
SyntaxError means the file wasn't valid JavaScript to begin with, so the engine never started executing any of it — not even statements that appear before the mistake.
Practice: warm-up
Open a browser console and, one at a time, evaluate: 2 + 2, let a = 10;, a, console.log(a).
For each, note whether the console shows a returned value, undefined, or nothing extra at all —
and why.
Stuck? Reveal one hint at a time.
Hint 1
A bare expression like 2 + 2 produces a value, and the console displays whatever value an evaluated line produces.
Hint 2
let a = 10; is a statement, not an expression — statements do not produce a displayable value.
Hint 3
console.log itself returns undefined, which is why the console shows undefined right after printing the greeting.
Reveal the expected console behavior
Try the problem yourself before reading this. There is often more than one reasonable approach — treat this as one worked example, not the only correct answer.
2 + 2 -> 4 (an expression's value)
let a = 10; -> (nothing shown — a statement, not a value)
a -> 10 (a is now an expression referring to the binding)
console.log(a) -> prints 10, then shows undefined (console.log's own return value)Practice: apply it
Save this as greet.js and run it with node greet.js. It’s broken — predict the error type before
you fix it, then fix it.
function greet(name) {
console.log('Hello, ' + name + '!'
}
greet('Ada');
Or reveal the answer without checking
Answer:SyntaxError, because a closing parenthesis is missing
console.log('Hello, ' + name + '!' is missing the closing ) for console.log's argument list, so the file fails to parse as valid JavaScript before anything runs.
Modification challenge: add the missing ), run it again, then change the greeting to also
print the length of name.
Summary
- ECMAScript is the language; browsers and Node each add their own host APIs on top of it — code using only language features behaves identically everywhere.
- An expression produces a value; a statement is a complete instruction that may contain expressions but isn’t one itself.
- Automatic Semicolon Insertion fills in missing semicolons using specific rules — most notably,
a line break right after
returnends the statement there, which is why this lesson writes semicolons explicitly rather than relying on it. SyntaxErrormeans the file never became valid JavaScript, so nothing in it ran.ReferenceErrormeans it parsed fine but referenced a binding that doesn’t exist at that point.- Reading the error type first tells you which category of problem to look for before you read the rest of the message.