JavaScript
2 min read
Updated 4 Aug 2026
Part 3 — Syntax
Comments, Semicolons and ASI, Identifiers, keywords, naming, Strict mode, Literals, expressions, statements
Comments#
// single line
/* multi
line */
/** JSDoc — tooling reads these for types & hints
* @param {number} x
* @returns {number}
*/
Semicolons and ASI#
JavaScript has Automatic Semicolon Insertion. You can omit most semicolons, but the rules bite in a few cases:
// ⚠️ ASI trap 1: return with a newline
function f() {
return
42; // returns undefined! ASI inserts ; after return
}
// ⚠️ ASI trap 2: line starting with ( [ ` / + -
const a = 1
const b = 2
[a, b].forEach(x => console.log(x)) // parsed as: const b = 2[a, b].forEach(...) → crash
🟢 Best practice: pick a style and let Prettier enforce it. If you omit semicolons, know the "leading ([ " gotcha and prefix such lines with ;`.
Identifiers, keywords, naming#
- Identifiers may contain letters, digits,
_,$, and start with anything but a digit. - Reserved words (
if,for,class,return,await,yield, …) can't be identifiers. - Case-sensitive:
name≠Name.
Conventions:
| Thing | Convention | Example |
|---|---|---|
| Variables / functions | camelCase |
userName, getUser() |
| Classes / constructors | PascalCase |
class UserAccount |
| Constants (true constants) | UPPER_SNAKE |
MAX_RETRIES |
Private (convention pre-#) |
leading _ 🔴 |
_secret |
| Truly private (modern) | # field |
#secret |
| Booleans | is/has/can prefix |
isActive, hasPaid |
Strict mode#
"use strict"; // top of file or top of a function
Why it exists: to remove sloppy, error-hiding behaviours. In strict mode:
- Assigning to an undeclared variable throws (instead of creating a global).
thisin a plain function call isundefined, not the global object.- Duplicate parameter names and octal literals are errors.
- Silent assignment failures (to read-only props) throw.
🟢 ES modules and class bodies are always strict — you rarely need to write "use strict" in modern code.
Literals, expressions, statements#
- Literal — a value written directly:
42,"hi",true,[1,2],{a:1},/re/g. - Expression — produces a value:
2 + 2,f(),x ? a : b. - Statement — performs an action:
if,for,let x = 1;. Statements don't produce values you can assign.
⚠️ Trap: {} at the start of a line is a block, not an object. {} + [] parses {} as an empty block, then +[] → 0. Wrap in parens to force object context: ({}) + [].