for..in in javascript

for..in in javascript

for..in loop retrieves the index value of the item or character one after the another from an array or string.

Initialization will always start at first position in an array.

Loop count will be determined by the array size.

Syntax

Example 1 - Read the Array value using for..in

Try Yourself

Example Explanation

  • In loop, first value(10) in the array is retrieved and stored in temp variable(var temp of ar).
  • Next that value stored in temp variable will be displayed along with its index value(alert("Index value of "+ar[temp]+" : "+temp);).
  • Then the second value(20) and its index value will be automatically retrieved and displayed.
  • The above steps continue till all the array values are retrieved and displayed.

Example 2 - Read the each characters from string value using for..of

Try Yourself

Difference between for..of and for..in loops

In for..of loop, the item or character from the array or string is retrieved.

But in for..in loop, the index value of the item in array or character in string is retrieved.

Example

Try Yourself