A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/for-in-loop-in-javascript/ below:

JavaScript For In Loop - GeeksforGeeks

JavaScript For In Loop

Last Updated : 20 Nov, 2024

The JavaScript for...in loop iterates over the properties of an object. It allows you to access each key or property name of an object.

JavaScript
const car = {
    make: "Toyota",
    model: "Corolla",
    year: 2020
};

for (let key in car) {
    console.log(`${key}: ${car[key]}`);
}

Output
make: Toyota
model: Corolla
year: 2020

Syntax

for (key in object) { // Code}

The for...in loop can also works to iterate over the properties of an array, but it is not recommended. for..in is mainly suitable for objects.

For arrays, we should use below loops.

JavaScript
// Example of for in for arrays
// Not a recommended way to traverse
// an array
const a = [1, 2, 3, 4, 5];

for (const i in a) {
  	console.log(a[i]);
}
Important Facts About for in Loop

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4