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";



2. Expression Statements

Expression statements are used to perform an action or calculation and can include variables, operators, and values.


Example:
 

let result = 5 * (10 + 3);
console.log(result); // Outputs: 65


3. Conditional Statements (if, else if, else)

Conditional statements allow you to execute different code based on specified conditions.


Example:
 

let age = 25;

if (age >= 18) {
    console.log("You are an adult.");
} else {
    console.log("You are a minor.");
}



4. Switch Statement

The switch statement allows you to choose one of many blocks of code to be executed.


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



5. Looping Statements (for, while, do-while)

Looping statements are used to repeat a block of code until a specified condition is met.


For Loop
 

for (let i = 0; i < 5; i++) {
    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



Do-While Loop
 

let num = 1;
do {
    console.log(num);
    num++;
} while (num <= 3);
// Outputs: 1, 2, 3



6. Break and Continue Statements

The break statement terminates the current loop, while the continue statement skips the current iteration and proceeds to the next one.


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

Function declaration statements define reusable blocks of code, while function invocation statements execute those functions.


Example:
 


function greet(name) {
    console.log(`Hello, ${name}!`);
}

greet("Alice"); // Outputs: Hello, Alice!
 



8. Try-Catch Statement

The try-catch statement is used for error handling to execute code and handle potential errors.


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

The return statement is used inside functions to return a value and exit the function.


Example:
 

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

let sum = add(5, 3);
console.log(sum); // Outputs: 8


 



Practice Excercise Practice now