JavaScript Loops - Exercise 4
The do...while loop in JavaScript is used to execute a block of code at least once, and then repeat the loop as long as a specified condition is true. The syntax is:
do {
// Code block to be executed
} while (condition);
You can use a do...while loop in JavaScript to execute a block of code at least once and then repeat the loop as long as a specified condition is true.
let i = 0;
do {
console.log(i);
i++;
} while (i < 3);
A labeled loop in JavaScript allows you to provide a label for a loop statement, which can be used with break or continue statements to control the flow of execution.
You can use a labeled loop in JavaScript by providing a label before the loop statement, and then using the label with break or continue statements to control the flow of execution.
outerloop:
for (let i = 0; i < 3; i++) {
innerloop:
for (let j = 0; j < 3; j++) {
if (i === 1 && j === 1) {
break outerloop;
}
console.log(i, j);
}
}
The typeof operator in JavaScript is used to determine the data type of a variable or expression.
You can use the typeof operator in JavaScript by placing it before the variable or expression you want to evaluate. It returns a string indicating the data type of the operand.
typeof 42; // Returns "number"
typeof "hello"; // Returns "string"
typeof true; // Returns "boolean"
A switch statement in JavaScript is used to perform different actions based on different conditions.
You can use a switch statement in JavaScript to evaluate an expression and execute a corresponding case block based on the value of the expression.
let day = 2;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
default:
console.log("Other day");
}
The main difference between a for loop and a while loop in JavaScript is the syntax and the way they are used. A for loop is typically used when you know the number of iterations beforehand, while a while loop is used when you want to continue looping until a certain condition is met.
The fall-through behavior of a switch statement in JavaScript allows the execution to continue to the next case block after the matching case, unless a break statement is encountered.