JavaScript Course Lesson 3 of 16
The Codex / JavaScript / Syntax & Statements

Syntax & Statements

The rules JavaScript uses to read your code — semicolons, comments, blocks, and the ASI trap.

What it is, in plain English: Syntax is the grammar of a programming language — the rules it uses to understand what you wrote. In JavaScript, statements end with a semicolon (optional but recommended), blocks of code live inside curly braces { }, and comments let you leave notes. Unlike Python, indentation doesn't matter — JavaScript uses braces to group code.

How JavaScript reads your code

Before JavaScript runs your program, it reads it — like a reader scanning a page. It needs your code to follow certain rules so it knows where one instruction ends and the next begins. These rules are called syntax.

Statements

A statement is one complete instruction. Think of it like a sentence. In English, sentences end with a full stop. In JavaScript, statements end with a semicolon.

console.log("Hello");   // one statement
let x = 5;              // another statement
x = x + 1;             // another statement

JavaScript can usually figure out where statements end even without semicolons (this is called ASI — see the Intermediate tab). But beginners should always write semicolons to avoid subtle bugs. Every statement in this course has one.

Comments

Comments are notes you leave in code. JavaScript ignores them completely — they're for humans.

// This is a single-line comment
// Everything after // on this line is ignored

let price = 9.99;   // you can put a comment after code too

/*
  This is a multi-line comment.
  Useful for longer explanations
  or temporarily disabling a block of code.
*/

let tax = price * 0.1;   /* also works inline */

In this course, every code example has comments explaining what each line does and why. Write comments like this in your own code — your future self will thank you.

Blocks: grouping code with curly braces

A block is a group of statements wrapped in curly braces { }. This is how JavaScript knows which statements belong together — inside an if, a loop, a function.

// An if statement uses a block:
if (temperature > 30) {
  // everything inside these braces runs only if the condition is true
  console.log("It's hot!");
  console.log("Drink water.");
}

// A function uses a block:
function greet() {
  // everything in here runs when greet() is called
  let message = "Hello!";
  console.log(message);
}
Key difference from Python: Python uses indentation to group code. JavaScript uses curly braces. Indentation in JavaScript is just style — it doesn't affect what the code does. The braces do.

Case sensitivity

JavaScript is case-sensitive. myName, MyName, and MYNAME are three completely different things. A common beginner mistake:

let myName = "Alice";
console.log(myName);   // works ✓
console.log(MyName);   // ReferenceError: MyName is not defined ✗

Whitespace and indentation

JavaScript ignores extra whitespace. These two blocks do exactly the same thing:

// Well-formatted (do this):
if (x > 0) {
  console.log("positive");
}

// Terrible but valid (don't do this):
if(x>0){console.log("positive");}

Always use consistent indentation — 2 spaces is the JavaScript convention (Python uses 4). Not because JavaScript cares, but because other humans need to read your code.

Naming things: identifiers

Names for variables, functions, and other things are called identifiers. The rules:

  • Must start with a letter, underscore _, or dollar sign $
  • Can contain letters, numbers, _, $ (but not start with a number)
  • Cannot be a reserved word like let, if, function
  • JavaScript convention: camelCase for variables and functions (myVariable, getUserName)
let firstName = "Alice";   // ✓ camelCase (JS convention)
let _private = 42;         // ✓ underscore prefix (used for "private" by convention)
let $price = 9.99;         // ✓ dollar sign (jQuery used this heavily)
let 2fast = true;          // ✗ SyntaxError: cannot start with a number
Try It Yourself

Official Sources

The Codex links only to official documentation and the ECMAScript specification. No third-party tutorials.