for of in javascript

for..of in javascript

for..of loop retrieves 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.


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

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(alert(temp);).
  • Then the second value(20) 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

Example 3 - Read the Array object value using for..of

Try Yourself