An array named `name` prints a character by string, not by word. use for cycle

I am trying to create a console log named name in JavaScript, but it does not work properly. The version I'm trying to run in chrome 29 , but without problems with firefox there is a restriction on its use.

Printing an array named name for the loop:

 var name = ["Hello", "foo", "test", "tried"]; for (var i = 0; i < name.length; i++) { console.log(name[i]); } 

Result: enter image description here

Printing an array named test for the loop:

 var test = ["Hello", "foo", "test", "tried"]; for (var i = 0; i < test.length; i++) { console.log(test[i]); } 

Result: enter image description here

+4
source share
1 answer

name is short for window.name , which is a property that contains the name of the current window. Following:

 var name = ["Hello", "foo", "test", "tried"]; 

does not create a new variable. Instead, the array is smoothed into a string, and the result is assigned to the name property:

 > var name = ["Hello", "foo", "test", "tried"]; undefined > name "Hello,foo,test,tried" 

This causes weird iteration behavior.

+4
source

Source: https://habr.com/ru/post/1500148/


All Articles