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 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)
// 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.
let y = 10;
console.log(x + y);
let y = 10
console.log(x + y)
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.
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
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.
name, _count, $priceInvalid:
1score, -valueuser1, first_name, is$validInvalid:
my-var, user namelet, for, class, returnUse descriptive names instead.
count and Count are different identifiers. Convention: use camelCase
for variables.// ✅ 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):
Future reserved words (avoid using as identifiers):
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.
// 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.
return a + b;
}
let result = add(5, 3);
Try It Yourself
Experiment with statements and syntax. Try intentionally breaking the naming rules to see the error messages in the preview console.
Which of the following is a valid JavaScript identifier?
- 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
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.