

In this loop, the variable i automatically receives the index so that each character can be accessed using str.Įxample: // for.


When the next () method is called on the iterator key value pairs are generated. An iterator object knows how to access items from an iterable one at the time, while keeping track of its current position within that sequence. array.entries returns a new Array iterator object. This is different from the ordinary for loop that we studied before. In a for.of loop we can achieve this via array.entries (). There exists a special form of the loop: for.in. To walk over all the characters of a string, we can use an ordinary for loop, with a loop counter ( i) to go through string index from 0 to str.length: // ordinary for loop let str = "Buzz" Use the string index number to loop through a string for loop There is no need to add brackets after it. Please note that str.length is a numeric property, not a function. The length property has the string length, it simply returns the number of characters in the string: let str = "hello123" Īlert(str.length) // 8 // the last character Looping Through a String The length Property
