JavaScript for-in Loop
Last Updated :
30 Jul, 2024
The for-in loop in JavaScript iterates over the enumerable properties of an object, executing a specified block of code for each property. It’s commonly used for iterating through object properties or elements in an array.
Syntax:
for (let i in obj1) {
// Prints all the keys in
// obj1 on the console
console.log(i);
}for in Loop Important Points
- Use the for-in loop to iterate over non-array objects. Even though we can use a for-in loop for an array, it is generally not recommended. Instead, use a for-of loop for looping over an array.
- The properties iterated with the for-in loop also include the properties of the objects higher in the Prototype chain.
- The order in which properties are iterated may not match the properties that are defined in the object.
for-in Loop Examples
Example: A simple example to illustrate the for-in loop over an array.
JavaScript
const array = [1, 2, 3, 4, 5];
// Using for-of loop for array
for (const value in array) {
console.log(array[value]);
}
Example 2: For-in loop iterates over the properties of an object and its prototype chain’s properties. If we want to display both properties of the “student1” object which belongs to that object only and the prototype chain, then we can perform it by for in loop.
javascript
const courses = {
// Declaring a courses object
firstCourse: "C++ STL",
secondCourse: "DSA Self Paced",
thirdCourse: "CS Core Subjects"
};
// Creating a new empty object with
// prototype set to courses object
const student1 = Object.create(courses);
// Defining student1 properties and methods
student1.id = 123;
student1.firstName = "Prakhar";
student1.showEnrolledCourses = function () {
console.log(courses);
}
// Iterating over all properties of
// student1 object
for (let prop in student1) {
console.log(prop + " -> "
+ student1[prop]);
}
Outputid -> 123
firstName -> Prakhar
showEnrolledCourses -> function () {
console.log(courses);
}
firstCourse -> C++ STL
secondCourse -> DSA Self Paced
thirdCourse -> CS Core Subjects
Supported Browsers:
The browsers supported are listed below:
JavaScript for-in Loop – FAQs
What is a for-in loop in JavaScript?
A for-in loop is a control structure that allows you to iterate over the enumerable properties of an object. This loop is used to access the keys of an object one by one.
What is the basic syntax of a for-in loop?
The basic syntax of a for-in loop includes the for keyword, followed by a variable to hold each key, the in keyword, and the object to iterate over. The code block to be executed for each key is placed inside curly braces.
How does the for-in loop work?
The for-in loop iterates over the enumerable properties of an object. For each property, the loop assigns the property key to the specified variable and executes the code block.
What is the difference between for-in and for-of loops?
- for-in loop: Iterates over the enumerable properties (keys) of an object.
- for-of loop: Iterates over the values of an iterable object (such as arrays, strings, maps, etc.).
Can a for-in loop be used with arrays?
Yes, a for-in loop can be used with arrays, but it is not recommended because it iterates over all enumerable properties, including inherited properties. It is better to use a for-of loop or a traditional for loop for arrays.
Please Login to comment...