Control Structures
Control Structures
Answer:
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
2
} else {
console.log('It is not an apple.');
}
switch Statement: Used to perform different actions based on different
conditions. It's an efficient alternative to multiple if statements when dealing with
many conditions.
let color = 'blue';
switch(color) {
case 'red':
console.log('The color is red.');
break;
case 'blue':
console.log('The color is blue.');
break;
default:
console.log('Color not recognized.');
}
Answer:
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
3
A while loop in JavaScript repeatedly executes a block of code as long as a
specified condition is true.
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
This code will print numbers from 0 to 4.
Answer:
A for loop is used for repeating a block of code a known number of times.
for (let i = 0; i < 5; i++) {
console.log(i);
}
This example will print numbers from 0 to 4.
Answer:
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
4
The do...while loop is a variant of the while loop. This loop will execute the code
block once, before checking if the condition is true, then it will repeat the loop as
long as the condition is true.
let i = 0;
do {
console.log(i);
i++;
} while (i < 5);
This will print numbers from 0 to 4.
Answer:
The break statement is used to exit from a loop or switch statement.
for (let i = 0; i < 10; i++) {
if (i === 5) {
break;
}
console.log(i);
}
This loop will print numbers from 0 to 4 and then exit.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
5
Question: How is the continue statement used in
JavaScript loops? Provide an example.
Answer:
The continue statement skips the current iteration of a loop and continues with
the next iteration.
for (let i = 0; i < 5; i++) {
if (i === 3) {
continue;
}
console.log(i);
}
This will print 0, 1, 2, and 4, skipping 3.
Answer:
Nested loops are loops inside another loop. They are often used for working with
multi-dimensional arrays or complex logic.
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
6
console.log(i + "," + j);
}
}
This will print pairs of numbers from (0,0) to (2,2).
Answer:
An if-else if-else ladder is used to execute one block of code among multiple
conditions.
let score = 75;
if (score > 90) {
console.log('Grade A');
} else if (score > 75) {
console.log('Grade B');
} else {
console.log('Grade C');
}
This will output 'Grade C' as the score is 75.
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
7
Question: Explain the purpose of the try-catch block in
JavaScript.
Answer:
The try-catch block is used for error handling. Code in the try block is executed,
and if an error occurs, the catch block is executed.
try {
nonExistentFunction();
} catch (error) {
console.log('An error occurred: ' + error.message);
}
This code will catch and display the error message.
Answer:
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
8
This will print each property and its value of the person object
Learn more about JavaScript with Examples and Source Code Laurence Svekis
Courses https://basescripts.com/
9