JavaScript Syntax

Updated · 6 min read
easyES2026javascriptsyntaxsemicolonscommentsbasics

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, Fee and FEE are three different names.
  • Comments are // one line and /* 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.

statements.js▶ Try it
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 too
Output
132
KindExamplesProduces a value?
Declaration statementconst x = 1; let y; function f() {}No
Control statementif (...) {} for (...) {} return x;No
Expression statementconsole.log("hi"); count++;Yes, but it is discarded
Expression2 + 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.

block.js▶ Try it
const balance = 40;
if (balance > 0) {
  console.log("Payment due");
  console.log("Amount:", balance);
}
Output
Payment due
Amount: 40

Semicolons 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:

asi-ok.js▶ Try it
const a = 1;
const b = 2
console.log(a + b)
Output
3

ASI 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:

asi-return.js▶ Try it
function getFee() {
  return
    120;
}
console.log(getFee());
Output
undefined

Explanation: 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.

case.js▶ Try it
const studentName = "Srihitha";
const StudentName = "Advitha";
console.log(studentName, StudentName);
Output
Srihitha Advitha

Two 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.

comments.js▶ Try it
// 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 */);
Output
100

Good 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:

whitespace.js▶ Try it
const total=120+30;
const sameTotal = 120 + 30;
console.log(total === sameTotal);
Output
true

Whitespace 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.

broken.js
function feeLine(name, fee) {
  return
    name + " owes $" + fee;
}
console.log(feeLine("Navika", 120));
Show solution
fixed.js▶ Try it
function feeLine(name, fee) {
  return name + " owes $" + fee;
}
console.log(feeLine("Navika", 120));
Output
Navika owes $120

Quick check: JavaScript syntax

3 questions
1Which of these is an expression?
2What does a function return if its body is return, a line break, then 120;?
3Which pair names two different variables?

Key Takeaways

SyntaxMeaning
;Ends a statement
{ ... }A block of statements
// textSingle-line comment
/* text */Multi-line comment
/** text */JSDoc comment read by editors

Was this tutorial helpful?

Lesson 5 of 139