23

When I typed this apparently innocent snippet of code:

values.name

gedit highlighted name as a keyword. However, name is not listed by the pages linked to by an answer to a question about reserved keywords. I also did a couple trivial tests in SpiderMonkey, but name seemed to act like an ordinary identifier.

A Google search didn't tell me much either. However, I did find a page listing name in "Other JavaScript Keywords". My guess is that name is a function or a member of some DOM element and does not intrude on the namespace.

Is name really a keyword in JavaScript? If so, what does it do?

Community
  • 1
  • 1
Joey Adams
  • 41,996
  • 18
  • 86
  • 115
  • `name` is (was) used as an attribute of the `a` element, so it could occur in DOM code. Maybe this is the reason your editor is highlighting it. – Thomas Apr 18 '10 at 20:05
  • Actually gedit highlights `name` as a property. But in the classic color scheme properties and keywords are using the same style definition. Because the parsing capabilities of gedit are quite limited though only a small set of important property names get highlighted with this style, for example regexp properties like `global`, `source`, `lastIndex` and function properties like `prototype`, `length` and `name`. – Robert Dec 31 '14 at 04:15
  • 1
    In chrome, name is behaving strangely like global object from localhost. i.e Within a function var name ='...' is modifying original window.name and therefore 'name; is available outside the fn scope. However when I run it separately in console and Plunkr its showing undefined/or Original window Object. In IE its behaving normal(i.e undefined/empty in localhost) – Amitesh Nov 13 '17 at 09:20

7 Answers7

21

Its not a javascript reserved word, its an html attribute. Any DOM element can have a name. Looks like your syntax editor will still highlight it.

James Westgate
  • 11,306
  • 8
  • 61
  • 68
  • 1
    It is not a `reserved word` but it is one of `JavaScript built-in objects, properties, and methods`: https://www.w3schools.com/js/js_reserved.asp – Robin Huy Mar 29 '18 at 09:43
8

(I know this was asked 2 years ago but, ...) This happened to me also, for example this below would not work.

name = document.getElementById('nombre');
//something else
name.className = 'thinking';

Instead I changed it to

username = document.getElementById('nombre');
//something else
username.className = 'thinking';

and it did work! Yeah, alright that's all, but it's something I find maybe quite interesting, also because of the 'name' attribute of the 'a' tag. Something to watch out for.

Katarot
  • 141
  • 2
  • 7
  • The question isn't really about problems with reusing the global `name` variable. In contrast to using a plain `name` in global scope the usage of `values.name` *does* work, it's just displayed abnormally in the text editor. – Robert Dec 31 '14 at 04:26
  • 2
    `window.name` has a setter which stringifies the value before storing it. Just declare it with `var` in a non-global scope. – Oriol Apr 02 '15 at 19:52
5

What?
'name' is the property of the window object of the browser. It is a built-in property in JavaScript.
Visit: https://developer.mozilla.org/en-US/docs/Web/API/Window/name

Why @deprecated
window.name is still in use in JavaScript, but it is planned to be removed in the future.
Visit: The Difference Between Deprecated, Depreciated and Obsolete

1

It's not a reserved word it's a variable (it is window.name) I'm not sure what it's defined by though.

Scott Warren
  • 243
  • 3
  • 12
  • 1
    The mentioned text editor does not highlight `name` though. It only highlights it when it's used as a property name like in `window.name`. Then only the `.name` part gets highlighted. – Robert Dec 31 '14 at 04:31
0

This happened to me also, for example, the following didn't work for me while I was testing the call, bind functions.

var name={
firstname: 'Abhishek',
lastname: 'Agarwal',

fullname: function{
    var fname = this.firstname + ' ' + this.lastname;
    return fname;
}

function printname(){
    console.log(this.fullname());
}
var final=printname.bind(name);
final();

This didn't actually perform the task and instead when I replaced "name" occurences by "myname" or literally anything else, it did work!

  • 1
    Interesting, what browser were you using? Your code snippet has some syntax errors, like missing end brace for var name, and `function` not followed by parens. [I cleaned up the syntax and got it to work with name](https://tio.run/##RY7LEoIwDEX3/YrsaAdl7ciw8FMC8qjW1GkLLhy@vfYBmGwy9@bm5IEL2s7ItzsvF@8XNED46qGBL4NQgzTWReUKxa2dpJ36Z3FKlsK/M6L5oApGDs1KZWeYqXNSExfbvVgRMmwUF05WBwRKKEKXWd4B9ZE0vZsN5XBWV7bWjO0YeBtJKcNF5nWarFZ9pfTIM2v7jQtRs5Wx9IwkVM2RrVpJdx6nsJI8Hgbvfw). – Joey Adams Apr 24 '20 at 12:18
0

strangely for me too, below code did not work for me when I am learning about call, apply and bind in javascript. I was breaking my head for half an hour till I figured out I am not doing any logical mistake but in the variable name lies the problem.

var name = {firstName: 'sarwan',lastName :'kumar'};

function say(){ console.log('' + this.firstName + ' ' + this.lastName); }

say.call(name);

This gives me the result as

"undefined undefined"

And I was like literally breaking my head suspecting somehow this is not passed to say function, tried changing other parts of code except the name variable, still did not work for me. But later figured out somehow, at the expense of spoiling my mood for good half an hour.

Sarwan
  • 595
  • 3
  • 8
  • 21
0

Using the Visual Studio code editor while having enabled Eslint,you can see that 'name' is classified as '@deprecated'. Although from Google it is not clear whether 'name' is a reserved keyword or not, the '@deprecated' label hints that the feature in question however is correct maybe as an effort to support backward compatibility ... etc, currently the feature has been disapproved of and one is encouraged to avoid.