![]() ![]() In this loop, the variable i automatically receives the index so that each character can be accessed using str.Įxample: // for. This is different from the ordinary for loop that we studied before. 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. If JavaScript encounters a break statement inside of a while or a for loop. Please note that str.length is a numeric property, not a function. There is one more way of stopping loops that is very useful and is used heavily. ![]() 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 ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |