Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's special about the "name" variable in JavaScript? [duplicate]

In the Chrome Developer Tools window, I typed in:

> name = ["a", "b", "c"]
["a", "b", "c"]

However, name became a string:

> typeof name
"string"
> name
"a,b,c"
> name[1]
","

This obviously isn't true for other variable names!

> foo = ["a", "b", "c"]
["a", "b", "c"]
> typeof foo
"object"
> foo[1]
"b"

And name is defined as the empty string on page load (and, as far as I can tell, cannot become anything other than a string).

So, what's up with name?

like image 515
kristina Avatar asked Sep 25 '13 15:09

kristina


People also ask

What is a variable name in JavaScript?

Variable names in JavaScript are often known as identifiers. Identifiers are a sequence of characters in a program that identifies a variable, function, or property. These are the names we use in JavaScript as a name to declare any variable.

How to use variables in JavaScript to store data?

We can use variables to store goodies, visitors, and other data. To create a variable in JavaScript, use the let keyword. The statement below creates (in other words: declares) a variable with the name “message”: Now, we can put some data into it by using the assignment operator =:

What are JavaScript identifiers?

These unique names are called identifiers. Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume). The general rules for constructing names for variables (unique identifiers) are: Names can contain letters, digits, underscores, and dollar signs. JavaScript identifiers are case-sensitive.

Are JavaScript variable names valid or invalid?

For example, break or boolean variable names are not valid. JavaScript variable names should not start with a numeral (0-9). They must begin with a letter or an underscore character. For example, 123test is an invalid variable name but _123test is a valid one. JavaScript variable names are case-sensitive.


1 Answers

When you type name you are implicitly referencing window.name, which according to MDN:

Gets/sets the name of the window.

https://developer.mozilla.org/en-US/docs/Web/API/window.name

like image 174
Alex W Avatar answered Oct 02 '22 19:10

Alex W