LEARN JAVASCRIPT
🚀 Dive into the world of JavaScript Loops! 🌐✨
Quiz JavaScript Loops!
10 insightful coding questions with detailed answers and explanations. Whether you’re a beginner or looking to refresh your skills, these exercises are perfect for you! 💻🎓
From basic for loops to intricate nested loops, we cover:
- Basic for loop syntax
- Creating infinite loops with while
- Backward counting with loops
- Iterating over arrays
- Using for…of for easy iteration
- The power of break in loops
- Skipping iterations with continue
- Printing even numbers with while
- Looping through object properties
- Mastering nested loops
These exercises are a great way to test and refine your JavaScript knowledge. 📚👨💻
Question: What is the syntax of a basic for loop in JavaScript?
Answer:
Syntax: for (initialization; condition; increment) { /* code block */ }
Example:
for (let i = 0; i < 5; i++) {
console.log(i);
}
Explanation: This for loop initializes i to 0, runs until i is less than 5, and increments i by 1 in each iteration, printing numbers from 0 to 4.
Question: How do you create an infinite loop using while?
Answer:
An infinite loop can be created by setting a condition that always evaluates to true.
Example:
while (true) {
console.log(‘This loop will run forever.’);
}
Explanation: The condition true never changes, causing endless iterations. Be cautious with infinite loops as they can crash your program.
Question: Write a JavaScript loop that counts backwards from 10 to 1.
Answer:
You can use a for loop that decrements the counter.
Example:
for (let i = 10; i > 0; i–) {
console.log(i);
}
Explanation: This loop starts with i at 10 and decreases i by 1 each time until i is no longer greater than 0.
Question: How can you use a for loop to iterate over an array in JavaScript?
Answer:
Use the array’s length for the loop condition.
Example:
let fruits = [‘apple’, ‘banana’, ‘cherry’];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
Explanation: This loop iterates over each index of the fruits array and prints each fruit.
Question: Explain how to use a for…of loop in JavaScript. Give an example.
Answer:
The for…of loop iterates over iterable objects like arrays, strings, etc.
Example:
let colors = [‘red’, ‘green’, ‘blue’];
for (let color of colors) {
console.log(color);
}
Explanation: This loop directly retrieves the value (e.g., each color) from the colors array.
Question: How do you break out of a loop in JavaScript?
Answer:
Use the break statement.
Example:
for (let i = 0; i < 10; i++) {
if (i === 5) {
break;
}
console.log(i);
}
Explanation: When i equals 5, the loop terminates. Thus, it prints numbers from 0 to 4.
Question: What is the purpose of the continue statement in a loop?
Answer:
The continue statement skips the current iteration and proceeds to the next iteration.
Example:
for (let i = 0; i < 5; i++) {
if (i === 3) {
continue;
}
console.log(i);
}
Explanation: This loop skips the number 3 and prints 0, 1, 2, and 4.
Question: Write a JavaScript while loop that prints all even numbers from 2 to 10.
Answer:
Example:
let i = 2;
while (i <= 10) {
console.log(i);
i += 2;
}
Explanation: The loop starts from 2 and increases i by 2 in each iteration, printing all even numbers from 2 to 10.
Question: How would you loop through an object’s properties using a for…in loop?
Answer:
Example:
let person = {name: ‘Alice’, age: 30, job: ‘Developer’};
for (let key in person) {
console.log(key + ‘: ‘ + person[key]);
}
Explanation: The for…in loop iterates over each property in the person object, accessing the key and the corresponding value.
Question: Explain how to nest a for loop inside another for loop. Provide an example.
Answer:
Nested loops are used when dealing with multi-dimensional structures.
Example:
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
console.log(i + ‘,’ + j);
}
}
Explanation: The outer loop runs three times, and for each iteration of the outer loop, the inner loop also runs three times, resulting in a total of 9 iterations.