leadakp.blogg.se

Js for loop
Js for loop








js for loop

Let’s look at the situations that the JavaScript for.in loop is best suited to. Inside the loop, we’re rendering the key, or index of each character, and the character at that index. In the following example, we’re looping over the variable obj and logging each property and value: const obj = //Output // "0: H" // "1: e" // "2: l" // "3: l" // "4: o" // "5: !" When using for.in loop to iterate an object in JavaScript, the iterated keys or properties - which, in the snippet above, are represented by the key variable - are the object’s own properties.Īs objects might inherit items through the prototype chain, which includes the default methods and properties of Objects as well as Object prototypes we might define, we should then use hasOwnProperty.










Js for loop