2

This is a two part question: General, and Specific.

For the general: I often find myself wondering what constitutes a viable variable name in JavaScript? I know there are certain 'words' that can not be used as variables in JavaScript; But I have yet to come across either a list of non-viable variable names, or a rule to apply when creating a variable name. I usually err on the side of caution and use obscure names if I am unsure.

It would be nice to know, with certainty, what can be used as a JavaScript variable, and what can not be used.

Any advice?

For the specific: I am wondering if I can use href as a variable name in my JavaScript? Is it viable, or is it reserved?


Afterthought: Perhaps I can extend this question to encompass JavaScript function names as well. What names are viable, and which are reserved? If the two questions are related, I will edit to ask both.

Note: I am not asking which characters can be used in a JavaScript variable; That question is already answered here.

Community
  • 1
  • 1
stefmikhail
  • 6,877
  • 13
  • 47
  • 61
  • 1
    Functions are first-class values, they can be assigned to variables so... – Jan Sep 12 '11 at 17:21
  • 1
    ... which is to say, if a name is usable for variables it is usable for functions as well, and vice-versa. – ellisbben Sep 12 '11 at 17:31
  • 1
    You are most welcome. In a certain sense, all Javascript functions are variables and my preferred coding style reflects that: `var myFunction = function() { /* stuff */ }` – ellisbben Sep 12 '11 at 18:00
  • I’ve made a tool for this that checks if a given string is a valid variable name as per ECMAScript 5.1: http://mothereff.in/js-variables It will also warn you if the name you entered is a reserved word in an older ECMAScript version (i.e. if you should avoid it for backwards compatibility). – Mathias Bynens Aug 08 '12 at 06:17

4 Answers4

2

Uhm, actually, you can use any kind of name as a variable name.

Instead of referring to the variable by name, refer to it by array index, since all object properties in JS can be accessed by index*, as well as the fact that global variables are simply properties of the window object.

*a string index can contain literally any kind of character sequence

So the question in turn might be more on the lines of "should I use reserved words as variable names?" Common sense would say you shouldn't, except when such a name is actually related to the construct and you can't find a suitable replacement.

window['function'] = 2;
window['if'] = 4;
window['var'] = 8;
alert(window['function'] + window['if'] + window['var']);

Warning!

Reserved words are different from native functionality. Although in many cases you can use names used as reserved keywords as variables, native functionality can actually be overwritten. For instance Mr Sarris above mentioned Node, (which is a native function not a reserved keyword), you can actually overwrite it by doing window['Node'] = myNewThing;. This has been used in some cases to achieve "wrapper" or "hotfix" functionality, but it is not guaranteed to work in a cross-browser manner (eg; MSIE's console object).

Christian
  • 27,509
  • 17
  • 111
  • 155
1

You can find lists of reserved words in JavaScript.

href is certainly fine as a variable name because href is an attribute of an a tag and in no way conflicts with JavaScript naming.

If you are ever in doubt as to whether or not a variable is already in use you can always open the developer tools (F12 in most browsers), go to the console, and type in the name. In this case you'll get:

> href
x ReferenceError: href is not defined

Nothing is using it, so it is yours to use without problem.

Just for kicks if you did enter a reserved word it would look like:

> finally
x SyntaxError: Unexpected token finally

Or if it was a native but already taken word it might look like:

> Node
function Node() { [native code] }

(Node is already defined, and its a native function)

Simon Sarris
  • 62,212
  • 13
  • 141
  • 171
  • Thank so much for the prompt answer. What do you think of my "afterthought"? Are there similar rules when defining a function name? – stefmikhail Sep 12 '11 at 17:20
  • I have edited to show you a bit more about dealing with which functions are already taken (ie Node is already taken) – Simon Sarris Sep 12 '11 at 17:23
  • 1
    is there a way to protect against future reserved words - like of javascript 9.0 makes href a reserved word? – Billy Moon Sep 12 '11 at 17:24
  • 1
    No, but its extremely rare that such a case occurs. You'll notice that only 30% or so if JavaScript reserved words are actually used at the moment. The rest are for possible future implementations. In other words, the reserved word list is _already_ a "future reserved words list" – Simon Sarris Sep 12 '11 at 17:25
  • I really want to use "super" as a var >_ – ajax333221 Sep 12 '11 at 17:38
  • @Simon Sarris - Excellent answer. And props for the edit. I'm going to use this from now on to be sure of my variable names. – stefmikhail Sep 12 '11 at 17:39
  • It’s not really a good idea to rely on a specific implementation when checking if something is or is not a reserved word. Implementations have bugs, and what works in engine A might break in engine B. Best to use http://mothereff.in/js-variables and see if it displays a warning for your variable name of choice. – Mathias Bynens Aug 08 '12 at 06:16
1

rules for variable declaration (on codelifter)

href is ok to use

kasper Taeymans
  • 6,950
  • 5
  • 32
  • 51
  • Thanks for this more extensive list of JavaScript variable rules. I will use this in combination with Simon Sarris' link as reference. – stefmikhail Sep 12 '11 at 17:43
1

Every programming language has a list of reserved words.

These reserved words consist of the parts that constitute that programming language.

For JavaScript, things like for or function or if are reserved words, since these have special meaning in the language itself. As a rule of thumb you cannot re-use words as identifiers (names) that already have a meaning in that particular language.

The official language specification is a good place to look that up. For JavaScript see the ECMAScript specification, section 7.6.1 (section 7.6. clarifies the other rules that apply to identifier naming).

Your question whether href is okay to use in JS is easily answered by looking there.

Tomalak
  • 332,285
  • 67
  • 532
  • 628