An Interest In:
Web News this Week
- April 29, 2024
- April 28, 2024
- April 27, 2024
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
April 22, 2023 02:09 pm GMT
Original Link: https://dev.to/max24816/javascript-for-in-loop-5gne
JavaScript for in loop
In JavaScript, for...in
loop is similar to for loop to iterate over an objects or array elements. The loop allows you to perform an action on each property of an object or element of an array.
Syntax of the Javascript For In Loop
for(var property in object) { // code to be executed }
Here, property
is a string variable representing the property key, and object
is the object to be iterated over.
Iterating Over an Object Example
const numbers = { "one": 1, "two": 2, "three": 3, "four": 4, "five": 5}for (let num in numbers) { console.log(`${num}: ${numbers[num]}`);}
Output:
one: 1two: 2three: 3four: 4five: 5
Iterating Over an Array Example
const numbers = [1, 2, 3, 4, 5]; for (let index in numbers) { console.log(`Index: ${index} Value: ${numbers[index]}`); }
Output:
Index: 0 Value: 1Index: 1 Value: 2Index: 2 Value: 3Index: 3 Value: 4Index: 4 Value: 5
Original Link: https://dev.to/max24816/javascript-for-in-loop-5gne
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To