📝 What You’ll Learn
- What JavaScript statements are
- Types of statements commonly used
- How statements execute
- Best practices and common pitfalls
📄 What Is a JavaScript Statement?
A JavaScript statement is a single instruction that the browser executes. Think of it like a sentence in English — each statement tells the browser to do something.
let x = 5;
console.log(x);
Each line above is a statement: one declares a variable, the other prints it.
🔄 Types of Statements
Here are the most common types of statements in JavaScript:
1. ✅ Declaration Statements
Used to declare variables:
let name = "Alex";
const age = 30;
2. 🧠 Expression Statements
These evaluate an expression:
x = 5 + 10;
3. 📢 Output Statements
Used to display information:
console.log("Hello!");
4. 🧩 Conditional Statements
Control decision-making:
if (x > 10) {
console.log("x is greater than 10");
}
5. 🔁 Looping Statements
Used for repeating tasks:
for (let i = 0; i < 3; i++) {
console.log(i);
}
🧱 Multiple Statements on One Line
JavaScript allows you to write multiple statements on one line, separated by a semicolon (;
):
let a = 1; let b = 2; console.log(a + b);
While valid, this is not recommended for readability.
🚫 Statement Blocks
A block is a group of statements wrapped in {}
. Blocks are used in functions, conditionals, loops, etc.
{
let message = "Hi!";
console.log(message);
}
⚠️ Common Mistakes
- Forgetting the semicolon at the end of a statement (JavaScript usually handles it, but not always predictably)
- Not wrapping multiple statements inside
{}
when required - Misunderstanding expressions vs. statements (e.g., trying to declare a variable inside a conditional expression)
📘 Recap
- A statement is a complete instruction for the browser
- JavaScript has many types: declarations, expressions, loops, conditionals, etc.
- Use semicolons to separate statements, even when JavaScript sometimes allows you to skip them
- Group related statements in blocks
{}
for structure