
Let’s go through Statements in JavaScript in a structured way.
📜 Statements in JavaScript
A statement in JavaScript is a command that tells the computer to do something.
- Each statement is usually written on a new line.
- Statements can end with a semicolon ( ; ) (optional in JS, but recommended).
Example:
let x = 10; // Variable declaration statement
console.log(x); // Function call statement
1️⃣ Declaration Statements
Used to declare variables or constants.
var name = "Raj"; // Old way
let age = 20; // Modern block-scoped
const PI = 3.14; // Constant
2️⃣ Expression Statements
- Produces a value and assigns it or uses it.
let sum = 5 + 10; // Expression statement
console.log(sum); // Function call (also a statement)
3️⃣ Conditional Statements
Used to make decisions.
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}
switch (day) {
case 1: console.log("Monday"); break;
case 2: console.log("Tuesday"); break;
default: console.log("Other day");
}
4️⃣ Loop Statements
Used to repeat tasks.
for (let i = 0; i < 5; i++) {
console.log(i); // For loop
}
let n = 0;
while (n < 3) {
console.log(n); // While loop
n++;
}
5️⃣ Function Statements
Define reusable blocks of code.
function greet(name) {
console.log("Hello " + name);
}
greet("Raj");
6️⃣ Try-Catch (Error Handling)
Handle errors safely.
try {
let result = 10 / 0;
console.log(result);
} catch (error) {
console.log("Error occurred!");
}
7️⃣ Jump Statements
Control the flow of loops.
for (let i = 0; i < 5; i++) {
if (i === 2) continue; // skips 2
if (i === 4) break; // stops loop
console.log(i);
}
8️⃣ Other Common Statements
return
→ Ends function and sends back a value.throw
→ Throws an error.debugger
→ Stops execution for debugging in DevTools.
function add(a, b) {
return a + b; // return statement
}
✅ Summary
JavaScript statements include:
- 📦 Declaration →
var
,let
,const
- 🧮 Expression → calculations, assignments
- 🔀 Conditional →
if
,else
,switch
- 🔁 Loops →
for
,while
,do...while
- 📞 Functions →
function
,return
- ⚠️ Error Handling →
try...catch
,throw
- ⏭️ Jump →
break
,continue