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?

5

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.