Skip to content

Latest commit

 

History

History
252 lines (190 loc) · 5.74 KB

File metadata and controls

252 lines (190 loc) · 5.74 KB

100 JS Questions (Loops)

Q1. What is a loop? What are the types of loops in JS?

A loop is a programming way to run a piece of code repeatedly until a certain condition is met.

Loop Type Definition Syntax Example
for Executes a block of code a specified number of times. for (initialization; condition; increment/decrement) { // code to be executed } for (let i = 0; i < 5; i++) { console.log(i); }
while Executes a block of code as long as a specified condition is true. while (condition) { // code to be executed } let x = 0; while (x < 5) { console.log(x); x++; }
do-while Executes a block of code at least once, then repeats as long as a specified condition is true. do { // code to be executed } while (condition); let x = 0; do { console.log(x); x++; } while (x < 5);
for...of Iterates over the elements of an iterable object (e.g., arrays, strings). for (let element of iterable) { // code to be executed } let fruits = ["apple", "banana", "cherry"]; for (let fruit of fruits) { console.log(fruit); }
for...in Iterates over the properties of an object. for (let property in object) { // code to be executed } let person = { name: "Alice", age: 30 }; for (let prop in person) { console.log(prop + ": " + person[prop]); }

Q2. What is the difference between while and for loops?

For loop:

  • Iterates a block of code a specific number of times.
  • Better for conditions with initialization and increment because all can be set in one line of code.
//for loop
for (let i=0; i<5; i++){
    console.log(i);
}
//Output: 0 1 2 3 4

While loop:

  • Executes a block of code while a certain condition is true.
  • Better when there is only a condition, no initialization, and no increment.
//while loop
let j = 0

while (j<5) {
    console.log(j);
    j++
}
//Output: 0 1 2 3 4

Extras:

//condition only
while ("a"=="a"){
    console.log("Happy")
}
//Output: Happy (infinity)

Q3. What is the difference between while and do-while loops? V.IMP.

While loop:

  • Executes a block of code while a certain condition is true.
// while loop
let j = 0;

while (j < 5) {
  console.log(j);
  j++;
}

// Output: 0 1 2 3 4

Do-while loop:

  • Similar to the while loop, except that the block of code is executed at least once, even if the condition is false.
// do-while loop
let k = 0;

do {
  console.log(k);
  k++;
} while (k > 1);

// Output: 0

Q4. What is the difference between break and continue statements? V.IMP.

break statement:

  • Terminates the loop.
// break statement
for (let i = 1; i <= 5; i++) {
  if (i === 3) {
    break;
  }
  console.log(i);
}
// Output: 1 2

continue statement:

  • Skips the current iteration of the loop and moves on to the next iteration.
// continue statement
for (let i = 1; i <= 5; i++) {
  if (i === 3) {
    continue;
  }
  console.log(i);
}

// Output: 1 2 4 5

Q5. What is the difference between for and for...of loops in JS?

for loop:

  • Slightly more complex with more lines of code.
let arr = [1, 2, 3];

for (let i = 0; i < arr.length; i++) {
  console.log(arr[i]);
}

// Output: 1 2 3

for...of loop:

  • Much simpler and better for iterating arrays.
let arr = [1, 2, 3];

for (let val of arr) {
  console.log(val);
}

// Output: 1 2 3

Q6. What is the difference between for...of and for...in loops?

for...of loop:

  • Used to loop through the values of an object like arrays, strings.
  • Allows you to access each value directly, without having to use an index.
let arr = [1, 2, 3];

for (let val of arr) {
  console.log(val);
}

// Output: 1 2 3

for...in loop:

  • Used to loop through the properties of an object.
  • Allows you to iterate over the keys of an object and access the values associated by using keys as the index.
const person = {
  name: 'Happy',
  role: 'Developer'
};

for (let key in person) {
  console.log(person[key]);
}

// Output: Happy Developer

Q7. What is the forEach method? Compare it with for...of and for...in loops.

forEach() method:

  • Available on arrays or objects.
  • Iterates over each element of the array or object and performs some action on each element.
const array = [1, 2, 3];

//for...of loop
for (let item of array) {
  console.log(item);
}
// Output: 1 2 3

//forEach method
array.forEach(function(item) {
  console.log(item);
});

// Output: 1 2 3
const person = {
  name: 'Happy',
  role: 'Developer'
};

//for-in loop
for (let key in person) {
  console.log(person[key]);
}
// Output: Happy Developer

//forEach method
Object.values(person).forEach(value =>{
    console.log(value);
})
//Output: Happy Developer

Key differences:

  • forEach is a method specific to arrays and objects, while for...of and for...in are more general loop constructs.
  • for...of iterates over values, while for...in iterates over keys.
  • forEach doesn't provide a way to break out of the loop early, while for...of and for...in do.

Q8. When to use for...of loops and when to use the forEach method in applications?

  • The for...of loop is suitable when you need more control over the loop, such as using the break or continue statement inside.

  • The forEach method iterates over each element of the array and performs some action on each element.

const array = [1, 2, 3];

// for-of loop
for (let item of array) {
  console.log(item);
  if (item === 2) {
    break; // Exit the loop when item is 2
  }
}
// Output: 1 2

// forEach method
array.forEach(function(item) {
  console.log(item);
  if (item === 2) {
    break; // Error: Illegal break statement
  }
});