Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does JavaScript split() produce different output with different variable names?

Tags:

javascript

Following is some codes and output from the Chrome Developers' Console

Case 1:

var myarr = document.location.hostname.split(".");    //typed
undefined                                             //output
myarr[0]                                              //typed
"ptamz"                                               //output: ONE

Case 2:

var name = document.location.hostname.split(".");     //typed
undefined                                             //output
name[0]                                               //typed
"p"                                                   //output: TWO

Why are the two outputs (commented Output: ONE, and Output: TWO) different?

Screenshot:

enter image description here

like image 309
ptamzz Avatar asked Mar 21 '12 08:03

ptamzz


People also ask

What does split do in JavaScript?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

Can split take multiple arguments JavaScript?

To split a string with multiple characters, you should pass a regular expression as an argument to the split() function. You can use [] to define a set of characters, as opposed to a single character, to match.

How do you split an object in JavaScript?

JavaScript split() Method: String ObjectThe split() method is used to split a string object into an array of strings by breaking the string into substrings. separator: The character to separate the string. The separator itself is a string. If the separator is not present it returns the entire string.

How do you split a string with multiple separators?

Use the String. split() method to split a string with multiple separators, e.g. str. split(/[-_]+/) . The split method can be passed a regular expression containing multiple characters to split the string with multiple separators.


1 Answers

name is a property of window. It appears that when you try to set that property to an array, the keys are joined with a comma (the result of calling toString on an array). So you are actually setting the window.name property to the concatenation of each element of document.location.hostname.split("."), separated by commas.

Here's a screenshot from my Chrome console demonstrating what happens:

enter image description here

The reason name[0] then results in p is that you can access the characters of strings using square brackets:

name = "hello,world";
console.log(name[0]); //"h"

Edit

As others have mentioned, this will only be the case in the global scope. You are free to declare a variable named name inside a descendant scope. Although, obviously, omitting the var keyword in this case would still result in you accessing window.name:

function example() {
    var name = ["hello", "world"];
    console.log(name); //["hello", "world"]
}
like image 116
James Allardice Avatar answered Nov 08 '22 04:11

James Allardice