javascript - What is the meaning of a for/in loop on a string? -


is code valid javascript?

for (item in "abc") {       console.log(item);   }   

output:

0   1   2   

what meaning of for/in loop on string?

if think of string array of characters, purpose of looping on string iterate through each of characters.

in javascript for..in loops return keys in object or array, you're seeing array indices. more useful format be:

var str,     item; str = "abc"; (item in str) {     console.log(str[item]); } 

which output 'a', 'b', , 'c'.

is code valid javascript?

it now, there issues in past.

note older browsers, array indices weren't supported on strings, backwards compatible way iterate on characters in string is:

var str,     i; str = "abc"; (i = 0; < str.length; i++) {     console.log(str.charat(i)); } 

Comments

Popular posts from this blog

javascript - ScrollTo Effect (href to div) -

javascript - ng-class not responding to change in model in Angular? -

javascript - document.registerElement extending HTMLInputElement prototype while using custom tag name to avoid implicit browser style -