in Javascript

for…in statement to loop through the javascript object properties

for..in is used to iterate through the properties in our javascript objects.

The syntax

for (variable in object) {
    //do something with the variable
}

Example

"use strict"

var obj = {a:1, b:2, c:3};
 
for (var prop in obj) {
 console.log("obj." + prop + " = " + obj[prop]);
}

// Output:
// "obj.a = 1"
// "obj.b = 2"
// "obj.c = 3"

You may also use the for..in without using the var keyword if you do no have “use strict” at the top.

Write a Comment

Comment

Webmentions

  • Different ways of setting a value to a Javascript object – Muthukrishnan

    […] be looped”. This property is used to restrict the set property to show up when using the for…in […]