1

var name = ['name1', 'name2', 'name2'];
console.log(name, name.length);
// name1,name2,name2 17

AND

var names = ['name1', 'name2', 'name2'];
console.log(names, names.length);
// (3) ["name1", "name2", "name2"] 3

What's wrong with the variable name in JS?

Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
Kongulov
  • 1,156
  • 2
  • 9
  • 19

2 Answers2

2

window.name Gets/sets the name of the window.

string = window.name;
window.name = string;

The name of the window is used primarily for setting targets for hyperlinks and forms. Windows do not need to have names.

It has also been used in some frameworks for providing cross-domain messaging (e.g., SessionVars and Dojo's dojox.io.windowName) as a more secure alternative to JSONP. Modern web applications hosting sensitive data should however not rely on window.name for cross-domain messaging but instead rather utilize the postMessage API.

Don't set the value to something unstring since its get method will call the toString method.

Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44
1

This is because window.name is reserved. The type should be string. So when you declare name via var and assign an array, it will be implicitly converted to string.

You could try this code:

let name = ['name1', 'name2', 'name2']; console.log(name, name.length); // (3) ["name1", "name2", "name2"] 3

Because let variable will not be attached into window object.

You could refer this MDN link: https://developer.mozilla.org/en-US/docs/Web/API/Window/name.

Ricky Jiao
  • 531
  • 4
  • 10