JavaScript Basics and Syntax
What this domain covers
The exam checks whether you can read a short snippet and predict exactly what the engine does: how statements are separated, when a line is treated as a continuation of the previous one, and which constructs are legal at all. Precision matters more than breadth here.
Statements, expressions, and declarations
A program is a sequence of statements; expressions produce values. let x = 5; is a declaration whose initialiser is the expression 5. Questions often ask what a snippet evaluates to (an expression) versus what it logs (execution order).
Strict mode
'use strict'; at the top of a script or function body turns silent failures into errors: assigning to an undeclared variable throws ReferenceError, with is banned, duplicate parameter names are illegal, and this is undefined inside a plain function call instead of the global object. Modules and class bodies are strict automatically. A directive placed after other statements is just a string expression and has no effect.
Identifiers and automatic semicolon insertion
Identifiers may contain letters, digits, $ and _, cannot begin with a digit, and are case-sensitive (myVar and myvar are different bindings). Semicolons are optional because ASI inserts them at a line break only when the next token cannot continue the statement. A line beginning with (, [, or a backtick continues the previous expression.
Worked example
let a = 1
let b = 2
let total = a + b
(function () { console.log(total) })()
Reasoning: there is no semicolon after a + b, and the next line starts with (. Because ( can continue the expression, ASI does not fire. The engine parses a + b(function () {...})() and throws TypeError: b is not a function. Adding the semicolon fixes it.
Traps
- Assuming
{}blocks scopevar(they do not). - Believing a
'use strict'directive inside a function applies to the whole file. - Forgetting that
//comments out the rest of the line, and that block comments do not nest.