Technologies:
Tolerim
a month ago
What is the difference between for...of and for...in loops, and why can they be substituted for each other in certain cases?
let numbers = [1,2,3,4,5,6,7,8,9,10]
function displayOddNumbers() {
for (num in numbers) {
num % 2 != 0 ? console.log(num) : console.log();
};
};
function displayEvenNumbers() {
for (num in numbers) {
num % 2 == 0 ? console.log(num) : console.log();
};
};
function displayEvenNumbers() {
for (num in numbers) {
num % 2 != 0 ? console.log(numbers[num]) : console.log();
};
};
Answers(2)
Vogi
a month ago
Verified Answer
The main difference between for...in and for...of loops in JavaScript is that for...in loops through the properties of an object while for...of loops over the values of an iterable object such as arrays, strings, and NodeLists.
In your case, you used a for...in loop to iterate through an array of integers from 1-10 inclusive. However, this loop iterates over keys instead of values. That's why it didn't work correctly when you tried to list all even numbers from the same array.
On the other hand, a for...of loop helps you iterate through each element of an array. This loop is used to loop over strings and arrays. When you switched to a for...of loop or used the correct index, you were able to display all even numbers effectively.
I hope this helps! Let me know if you have any other questions.
Sources
javascript - What is the difference between ( for... in ) and ( for ...for...of - JavaScript | MDN - Mozilla Developerdifference between for loop and for-in loop in javascriptDifference between for...of and for...in loop in JavaScript.Explain the differences between for(..in) and for(..of) statement in ...Tolerim
a month ago
The difference between for...in and for...of loops in JavaScript is that for...in loops iterate over the properties of an object, while for...of loops iterate over the values of an iterable object such as an array.
In the first example, using a for...in loop to iterate over the array worked because the loop variable "num" was used as an index to access the elements of the array, but this is not recommended since "num" is actually a string representing the name of the property. In this case, it happened to work because the indices of the array were string representations of their integer values. However, the console output would have been the array indices (0-9) instead of the actual values.
In the second example, using the same for...in loop to iterate over the array did not work because the loop variable was again used as an index, but this time the values of the array were not accessed correctly. This is because the index values in the loop are strings, and using the modulus operator with a string will return NaN.
To fix this issue, we can use the for...of loop, which iterates over the values of the array directly, avoiding the need to access elements by index. Alternatively, we can use the parseInt() function to convert the loop variable to an integer before using the modulus operator.