Before the real work starts, a few basic rules about how JavaScript code is written.
Statements
A program is a list of statements, instructions that the engine runs one after another.
const price = 20;
const tax = 4;
console.log(price + tax); // → 24
Each line is a statement. The semicolon ; marks the end of one.
Semicolons
Semicolons are technically optional, because JavaScript has automatic semicolon insertion (ASI): it adds them where it thinks a statement ended. Many teams write them anyway, and this course does too, because there are a few surprising cases without them. The most famous one:
function getTotal() {
return
20 + 4;
}
Because of ASI, JavaScript puts a semicolon after return, so this function returns undefined rather than 24. Keep the returned value on the same line as return.
Comments
Comments are notes for people. The engine ignores them.
// A single-line comment
/* A comment that
covers several lines */
const total = 10 + 5; // a comment after code
console.log(total); // → 15
Use comments to explain why something is done, not to repeat what the code obviously says.
Case sensitivity
JavaScript treats upper and lower case as different.
const userName = "Maya";
console.log(userName); // → Maya
console.log(username); // error on purpose: not the same name
Run it. The console shows a ReferenceError, because username and userName are two different names. Reading these error messages is a skill, and you will practice it in Reading Console Errors.
Whitespace and strict mode
Extra spaces and line breaks do not matter, so format code so it reads well. Code inside ES modules (and inside classes) automatically runs in strict mode, a safer version of the language that turns some silent mistakes into real errors. Older scripts can switch it on with "use strict"; at the top. You usually do not need it in modern code that uses modules.