Javascript 命名为 "name" 的数组变量在存储字符串时显示意外结果

Javascript array variable when named "name" showing unexpected result when storing strings

我正在编写一个非常简单的代码,用于将字符串存储在数组中然后拆分它,但显然 "variable name" 似乎对结果有影响。

我已经在 Google Chrome 控制台和 Microsoft edge 上试过了。结果一样。

var fullName = "Jonathan Archer";
var name = fullName.split(" ");
console.log(name[0]);

//The output of the above code is : "J"

var userName = fullName.split(" ");
console.log(userName[0]);

//The output of the above code is: "Jonathan"

//Also tried following, also exhibited same behavior as above
var name = ["Jonathan", "Archer"];
var userName = ["Jonathan", "Archer"];
console.log(name[0]);
console.log(userName[0]);

我不明白为什么这两个代码片段会产生不同的结果。在JavaScript中使用"name"作为数组名有什么限制吗?

不要用name作为变量名,因为它会和window.name冲突

var fullName = "Jonathan Archer";
var n = fullName.split(" ");
console.log(n[0]);

//The output of the above code is : "J"

var userName = fullName.split(" ");
console.log(userName[0]);

//The output of the above code is: "Jonathan"

//Also tried following, also exhibited same behavior as above
var n = ["Jonathan", "Archer"];
var userName = ["Jonathan", "Archer"];
console.log(n[0]);
console.log(userName[0]);