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

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -