JavaScript For In Loop (original) (raw)

Last Updated : 20 Nov, 2024

TheJavaScript 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.

// 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

Similar Reads

JavaScript Basics







JS Variables & Datatypes






JS Operators













JS Statements







JS Loops







JS Perfomance & Debugging




JS Object







JS Function








JS Array