JavaScript Syntax
Syntax is the set of rules that decide how JavaScript code must be written so the engine can understand it, in the same way grammar decides how words make a sentence. A program is a list of statements, statements contain expressions, and comments and whitespace help people read them. This lesson covers those rules, including the one that surprises most people: JavaScript sometimes adds semicolons for you, and occasionally in the wrong place.
In One Minute
- A statement is an instruction (
const fee = 120;). An expression is anything that produces a value (120 * 2). - End statements with a semicolon. JavaScript inserts missing ones (automatic semicolon insertion), but not always where you expect.
- JavaScript is case-sensitive:
fee,FeeandFEEare three different names. - Comments are
// one lineand/* many lines */. Whitespace and line breaks are mostly ignored.
Statements and Expressions
A statement tells JavaScript to do something. A program runs its statements one after another, top to bottom.
An expression is a piece of code that evaluates to a value. 3 + 4 is an expression (its value is 7), so is "Navika", so is fee * 2. Expressions can appear inside statements wherever a value is expected.
const fee = 120; // a statement (a declaration)
const lateFee = fee * 0.1; // fee * 0.1 is an expression inside it
console.log(fee + lateFee); // fee + lateFee is an expression too132| Kind | Examples | Produces a value? |
|---|---|---|
| Declaration statement | const x = 1; let y; function f() {} | No |
| Control statement | if (...) {} for (...) {} return x; | No |
| Expression statement | console.log("hi"); count++; | Yes, but it is discarded |
| Expression | 2 + 3 fee > 100 "a" + "b" f(4) | Yes |
The difference matters later: some places accept only an expression. A template literal's ${...} must contain an expression, and an arrow function's short form must return one.
Blocks
Curly braces { } group statements into a block. Blocks are used by if, loops and functions. Statements inside a block are indented by convention (two spaces in this course), which the engine ignores but readers rely on.
const balance = 40;
if (balance > 0) {
console.log("Payment due");
console.log("Amount:", balance);
}Payment due
Amount: 40Semicolons and Automatic Semicolon Insertion
A semicolon ends a statement. If you leave one out, the parser often inserts it for you when a line break would otherwise make the code invalid. This is automatic semicolon insertion (ASI). So both of these work:
const a = 1;
const b = 2
console.log(a + b)3ASI has a famous trap. After the keyword return, a line break always ends the statement. Code that puts the value on the next line returns nothing:
function getFee() {
return
120;
}
console.log(getFee());undefinedExplanation: JavaScript reads this as return; followed by an unreachable 120;. The function returns undefined. The fix is to keep the value on the same line as return (or start a parenthesis there: return ().
Case Sensitivity and Names
Every name in JavaScript is case-sensitive. Keywords are all lowercase: const, if, function, return. Writing Const or IF is a syntax error or an unknown name.
const studentName = "Srihitha";
const StudentName = "Advitha";
console.log(studentName, StudentName);Srihitha AdvithaTwo different variables, because the first letters differ. In practice, never rely on case alone to tell names apart; it confuses readers. The naming rules and conventions are in the variables lesson.
Comments
Comments are notes for people. The engine skips them.
// A single-line comment runs to the end of the line.
const fee = 120; // It can follow code on the same line.
/*
A multi-line comment can span
several lines.
*/
const discount = 20;
console.log(fee - discount /* inline comments work too */);100Good comments explain why, not what. // subtract the discount repeats the code; // sibling discount applies from the second child tells the reader something the code can't.
Whitespace and Line Breaks
Spaces, tabs and line breaks between tokens are ignored, so these two lines mean the same thing:
const total=120+30;
const sameTotal = 120 + 30;
console.log(total === sameTotal);trueWhitespace inside a string is kept, and a token itself can't contain spaces (con st is not const). Long statements can be broken across lines after an operator or a comma.
Common Mistakes
Putting a value on the line after return. ASI turns it into return;. Keep the start of the value on the return line.
Mixing up capitalization. console.Log fails with TypeError: console.Log is not a function; the method is console.log.
Unbalanced brackets and quotes. Every (, [, { and quote needs its partner. The engine reports SyntaxError: Unexpected end of input when one is missing at the end of a file. Editors highlight matching pairs: click next to a bracket to see its partner.
Using smart quotes copied from a document. “Navika” uses typographic quotes, which aren't string delimiters, and causes SyntaxError: Invalid or unexpected token. Use plain " or '.
Practice
Challenge: The function below should return the full fee line, but it returns undefined. Fix it without changing the text it builds.
function feeLine(name, fee) {
return
name + " owes $" + fee;
}
console.log(feeLine("Navika", 120));Show solution
function feeLine(name, fee) {
return name + " owes $" + fee;
}
console.log(feeLine("Navika", 120));Navika owes $120Key Takeaways
| Syntax | Meaning |
|---|---|
; | Ends a statement |
{ ... } | A block of statements |
// text | Single-line comment |
/* text */ | Multi-line comment |
/** text */ | JSDoc comment read by editors |
Was this tutorial helpful?