Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS issue while assigning .split() result to a variable called 'name'

Tags:

javascript

I was making some tests with the JS's split() string method in Firefox's console. While testing I did the following:

test = 'first second third'.split(' ')
-> Array [ "first", "second", "third" ]
test
-> Array [ "first", "second", "third" ]
name = 'first second third'.split(' ')
-> Array [ "first", "second", "third" ]
name
-> "first,second,third"

I noticed that everytime I called the split() method it returned an Array but if I saved it in a variable called name then it was saved as a string rather than an Array.

Why does this happens?

like image 746
ale11 Avatar asked Jul 26 '26 21:07

ale11


1 Answers

This issue will only come from window.name, due to the way it's setter and/or getter is defined. If you use the variable name inside a function, you won't face this issue.

In case you're curious to see how window.name changes the output from the array to a string, here is a quick example I made: JSFiddle.

like image 199
Saad Avatar answered Jul 28 '26 10:07

Saad