Tutorials JavaScript Syntax & Statements
Ch 9 / 78
JavaScript Beginner Chapter 4

Syntax & Statements

Syntax is the set of rules that defines how JavaScript programs are written and interpreted. Understanding the rules about statements, semicolons, identifiers, and keywords is foundational to writing valid code.

What you'll learn in this chapter
What a JavaScript statement is
When to use semicolons (and ASI)
Case sensitivity rules in JS
Valid identifier naming rules
JavaScript reserved keywords
Code blocks and whitespace

What is a JavaScript Statement?

A statement is a single instruction that JavaScript executes. A program is a list of statements. Each statement performs one action: declare a variable, call a function, run a loop, etc.

let  score  =  100;
keyword (let)
identifier (variable name)
assignment operator
value / expression
semicolon (statement terminator)
JS — example statements
// Each line below is one statement
let x = 5;                         // declaration statement
x = x + 1;                         // expression statement
console.log(x);                    // function call statement

// Multiple statements on one line (legal, but avoid)
let a = 1; let b = 2; let c = a + b;

// A statement can span multiple lines
let message =
    "This is a long string that " +
    "spans multiple lines in the source.";

Semicolons & Automatic Semicolon Insertion

In JavaScript, semicolons ; terminate statements. They are technically optional thanks to Automatic Semicolon Insertion (ASI) — a JS engine mechanism that inserts semicolons for you in most cases.

✓ With semicolons (recommended)
let x = 5;
let y = 10;
console.log(x + y);
⚠ Without (ASI handles it, but risky)
let x = 5
let y = 10
console.log(x + y)
ASI Pitfall
ASI can sometimes insert a semicolon in the wrong place — especially with return, throw, and arrow functions. Always use explicit semicolons to be safe. Many style guides (and Prettier) enforce them automatically.

JavaScript is Case-Sensitive

JavaScript treats uppercase and lowercase letters as entirely different characters. name, Name, and NAME are three separate identifiers.

JS — case sensitivity
let name  = "Alice";    // lowercase n
let Name  = "Bob";      // uppercase N — different variable!
let NAME  = "Charlie";  // all uppercase — yet another variable

console.log(name);  // "Alice"
console.log(Name);  // "Bob"
console.log(NAME);  // "Charlie"

// Keywords are case-sensitive too
let x = 10;  // ✅ correct keyword
// Let x = 10;  // ❌ SyntaxError — 'Let' is not a keyword
Convention
JavaScript developers use camelCase for variable and function names (firstName, getUserName), PascalCase for classes (UserAccount), and UPPER_SNAKE_CASE for constants (MAX_RETRY_COUNT).

Identifiers — Naming Rules

An identifier is the name you give to variables, functions, classes, or any user-defined symbol. JavaScript has strict rules about what makes a valid identifier.

Must start with a letter, _ or $
Valid: name, _count, $price
Invalid: 1score, -value
Can contain letters, digits, _ or $
Valid: user1, first_name, is$valid
Invalid: my-var, user name
Cannot use reserved keywords
Invalid: let, for, class, return
Use descriptive names instead.
Case-sensitive
count and Count are different identifiers. Convention: use camelCase for variables.
JS — valid vs invalid identifiers
// ✅ Valid identifiers
let firstName = "Alice";
let _count    = 0;
let $price    = 9.99;
let user2     = "Bob";
const MAX_SIZE = 100;

// ❌ Invalid identifiers (will cause SyntaxError)
// let 1name  = "bad";   // starts with digit
// let my-var = "bad";   // hyphen not allowed
// let for    = "bad";   // reserved keyword
// let my var = "bad";   // space not allowed

Reserved Keywords

These words are part of the JavaScript language syntax and cannot be used as variable names. Memorising them will help you avoid naming conflicts.

Current keywords (part of the language today):

breakcasecatchclassconstcontinuedebuggerdefaultdeletedoelseexportextendsfalsefinallyforfunctionifimportininstanceofletnewnullreturnstaticsuperswitchthisthrowtruetrytypeofvarvoidwhilewithyield

Future reserved words (avoid using as identifiers):

enumimplementsinterfacepackageprivateprotectedpublic

Code Blocks { }

A code block is a group of statements enclosed in curly braces { }. Blocks are used with if, for, while, function, class, and other structures.

JS — code blocks
// if block — runs if condition is true
if (score > 90) {
    console.log("Excellent!");
    console.log("Grade: A");
}

// function block — reusable group of statements
function greet(name) {
    let message = `Hello, ${name}!`;
    console.log(message);
}

// Blocks can be nested
for (let i = 0; i < 3; i++) {
    if (i % 2 === 0) {
        console.log(i + " is even");
    }
}

Whitespace & Line Length

JavaScript ignores extra whitespace (spaces, tabs, newlines) between tokens. This means you can format code for readability without affecting how it runs.

✓ Readable formatting
function add(a, b) {
  return a + b;
}

let result = add(5, 3);
⚠ Minified (also valid)
function add(a,b){return a+b}let r=add(5,3);
Pro Tip
Use a code formatter like Prettier to automatically format your JS consistently. Most editors (VS Code, WebStorm) have Prettier built in or as a one-click install. Never manually count spaces — let the tool do it.
⚡
Free Resource
Download our JavaScript Syntax Quick Reference
Keywords, operators, control flow — all on one printable page.
Get Sheet

Try It Yourself

Experiment with statements and syntax. Try intentionally breaking the naming rules to see the error messages in the preview console.

Live Editor
✎ HTML / JS
👁 Preview
Quick Check — Chapter 4

Which of the following is a valid JavaScript identifier?

Chapter Summary
  • A statement is a single JS instruction — programs are lists of statements.
  • Semicolons terminate statements. ASI handles omissions but always use them explicitly.
  • JavaScript is case-sensitive — name ≠ Name ≠ NAME.
  • Identifiers must start with a letter, _ or $, and cannot be reserved keywords.
  • Use camelCase for variables/functions, PascalCase for classes, UPPER_SNAKE_CASE for constants.
  • Code blocks { } group statements for if, loops, and functions.

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel