When I run var name = [1,2,3]
in Chrome's console and then access the value of name
I get back "1,2,3"
. Why is this the case?
Asked
Active
Viewed 206 times
4

Pavan Ravipati
- 1,890
- 14
- 21
-
1It is not just `name`, also `status` does the same.... but only in the global scope... not in local scope inside functions – Arun P Johny Jul 24 '15 at 03:46
1 Answers
7
What you are seeing is a global variable that is part of the window
object. This is actually a value the browser uses that reflects the name of the window. (see documentation)
Since window.name
is a string getter/setter, your array is being cast to a string. (and the console operates in the "global scope", so var name
and window.name
are the same value. (if you were nested inside a function, this same behavior would not apply because it wouldn't be the global scope anymore)

Dominic Barnes
- 28,083
- 8
- 65
- 90