Syntax & Statements
Syntax is the set of rules for how JavaScript code is written: how statements end, how blocks are grouped, how names are spelled and how comments are marked. These rules are small in number, and getting comfortable with them now saves a lot of confusing errors later.
Statements
A program is a list of statements — instructions the engine runs one after another, top to bottom:
let price = 20; let quantity = 3; let total = price * quantity; console.log(total);
60
Each line here is one statement. Statements usually end with a semicolon ;.
Semicolons
JavaScript has a feature called automatic semicolon insertion: if you leave a semicolon off at the end of a line, the engine usually adds one for you. So both of these work:
let a = 1; let b = 2 console.log(a + b)
3
"Usually" is the important word. There are rare cases where a missing semicolon changes the meaning of the code. Many teams always write semicolons; others never do and rely on a formatter to catch the edge cases. Pick one style and let a tool like Prettier enforce it. This course uses semicolons.
Expressions vs statements
An expression is any piece of code that produces a value: 5, price * quantity, "Hi " + name. A statement performs an action, and often contains expressions. let total = price * quantity; is a statement containing the expression price * quantity.
Code blocks
Curly braces { } group statements into a block. Blocks belong to things like if statements, loops and functions:
let temperature = 31; if (temperature > 30) { console.log("It's hot."); console.log("Drink some water."); }
It's hot. Drink some water.
Indentation (usually two or four spaces) inside blocks doesn't change what the code does, but it makes structure visible at a glance. Always indent consistently.
Comments
Comments are notes for humans; the engine ignores them completely:
// A single-line comment runs to the end of the line. /* A multi-line comment can span several lines. */ let speed = 50; // comments can also follow code // console.log("this line is switched off"); console.log(speed);
50
Good comments explain why code exists, not what it does — the code already says what. "Commenting out" a line, as on the second-to-last line, is a quick way to disable it while testing.
Case sensitivity
JavaScript is case-sensitive: total, Total and TOTAL are three different names. Keywords such as let, if and function must be lowercase:
let score = 10;
console.log(Score);
Uncaught ReferenceError: Score is not defined
Identifiers and naming
Names for variables and functions are called identifiers. They can contain letters, digits, _ and $, but can't start with a digit, and can't be a reserved word like if, class or return.
| Style | Example | Used for |
|---|---|---|
| camelCase | firstName, getUserById | Variables and functions — the standard JavaScript style |
| PascalCase | ShoppingCart | Classes and constructors |
| UPPER_SNAKE_CASE | MAX_RETRIES | Fixed configuration constants |
Whitespace
Extra spaces, tabs and line breaks between tokens are ignored, so let x=1+2; and let x = 1 + 2; are identical. Use whitespace to make code readable: a space around operators, and blank lines between logical sections.
Try it yourself
This code has a bug caused by capitalisation. Fix it so it prints the greeting: