1. Variable Declaration and Assignment Statements
Variable declaration statements are used to create variables, while assignment statements are used to assign values to variables.
Example:
// Variable Declaration
let name;
// Assignment Statement
name = "John";
let name;
// Assignment Statement
name = "John";
2. Expression Statements
Example:
let result = 5 * (10 + 3);
console.log(result); // Outputs: 65
console.log(result); // Outputs: 65
3. Conditional Statements (if, else if, else)
Example:
let age = 25;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor.");
}
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor.");
}
4. Switch Statement
Example:
let day = 3;
let dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
// More cases...
default:
dayName = "Unknown";
}
console.log(dayName); // Outputs: Tuesday
let dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
// More cases...
default:
dayName = "Unknown";
}
console.log(dayName); // Outputs: Tuesday
5. Looping Statements (for, while, do-while)
For Loop
for (let i = 0; i < 5; i++) {
console.log(i);
}
// Outputs: 0, 1, 2, 3, 4
console.log(i);
}
// Outputs: 0, 1, 2, 3, 4
While Loop
let count = 0;
while (count < 3) {
console.log(count);
count++;
}
// Outputs: 0, 1, 2
while (count < 3) {
console.log(count);
count++;
}
// Outputs: 0, 1, 2
Do-While Loop
let num = 1;
do {
console.log(num);
num++;
} while (num <= 3);
// Outputs: 1, 2, 3
do {
console.log(num);
num++;
} while (num <= 3);
// Outputs: 1, 2, 3
6. Break and Continue Statements
Example:
for (let i = 0; i < 5; i++) {
if (i === 3) {
break; // Exits the loop when i equals 3
}
console.log(i);
}
// Outputs: 0, 1, 2
for (let i = 0; i < 5; i++) {
if (i === 2) {
continue; // Skips iteration when i equals 2
}
console.log(i);
}
// Outputs: 0, 1, 3, 4
7. Function Declaration and Invocation Statements
Example:
function greet(name) {
console.log(`Hello, ${name}!`);
}
greet("Alice"); // Outputs: Hello, Alice!
8. Try-Catch Statement
Example:
try {
// Code that may throw an error
let result = 10 / 0;
console.log(result);
} catch (error) {
console.log("An error occurred:", error.message);
}
// Outputs: An error occurred: Division by zero
9. Return Statement
Example:
function add(a, b) {
return a + b;
}
let sum = add(5, 3);
console.log(sum); // Outputs: 8
return a + b;
}
let sum = add(5, 3);
console.log(sum); // Outputs: 8
Practice Excercise Practice now