In JavaScript, a statement is an instruction that the browser executes. A JavaScript program consists of multiple statements that control how the code runs. For example:
let name = "Alice"; // A statement that declares a variable console.log(name); // A statement that prints output
When writing code, we often need to repeat actions multiple times. This is where loops come in. Loops allow us to execute a block of code repeatedly until a condition is met. For example, if we want to print numbers from 1 to 5:
for (let i = 1; i <= 5; i++) {
console.log(i);
}
Loops help make our programs efficient, organised, and easy to manage. Now, let’s explore different types of statements and loops in JavaScript.
JavaScript statements are instructions that the browser executes.
Examples:
– Variable declaration: let x = 10;
– Function call: console.log(“Hello”);
– Condition check: if (x > 5) { console.log(“Big number”); }
Statements are executed from top to bottom, one by one.
if: Runs a block of code if the condition is true. For example:
if (x > 5) { console.log("Greater than 5"); }
else: Runs a block of code if the condition is false. For example:
if (x > 5) { console.log("Greater than 5"); } else { console.log("5 or less"); }
else if: Checks multiple conditions. For example:
if (x > 10) { console.log("Greater than 10"); }
else if (x > 5) { console.log("Greater than 5"); }
else { console.log("5 or less"); }
The switch statement is used to check multiple conditions efficiently. For example:
switch (color) {
case "red":
console.log("Color is red");
break;
case "blue":
console.log("Color is blue");
break;
default:
console.log("Unknown color");
}
The for loop repeats a block of code a specific number of times. For example:
for (let i = 1; i <= 5; i++) {
console.log(i);
}
// Output: 1, 2, 3, 4, 5
Here is how you would structure a for statement:
for (initialisation; condition; increment) {
// Code to run
}
The while loop executes as long as a condition is true. It is used when the number of iterations is unknown. For example:
let i = 1;
while (i <= 5) {
console.log(i);
i++;
}
// Output: 1, 2, 3, 4, 5
The do-while loop runs at least once, then checks the condition. It is used when the code must run at least once. For example:
let i = 1;
do {
console.log(i);
i++;
} while (i <= 5);
// Output: 1, 2, 3, 4, 5
forEach is used to loop through arrays. forEach is simpler than a for loop for arrays. For example:
const numbers = [1, 2, 3, 4, 5];
numbers.forEach(num => console.log(num));
// Output: 1, 2, 3, 4, 5
break: Stops the loop immediately. For example:
for (let i = 1; i <= 5; i++) {
if (i === 3) break;
console.log(i);
}
// Output: 1, 2
continue: Skips the rest of the loop iteration and moves to the next. For example:
for (let i = 1; i <= 5; i++) {
if (i === 3) continue;
console.log(i);
}
// Output: 1, 2, 4, 5
Here’s an example combining different loops:
for (let i = 1; i <= 3; i++) {
console.log("for loop: " + i);
}
let j = 1;
while (j <= 3) {
console.log("while loop: " + j);
j++;
}
let k = 1;
do {
console.log("do-while loop: " + k);
k++;
} while (k <= 3);
// Output:
// for loop: 1, 2, 3
// while loop: 1, 2, 3
// do-while loop: 1, 2, 3
Ready to start mastering JavaScript operators? Explore more tutorials on HowCodingWorks and level up your coding skills!