In high - level programming languages, identifiers are mostly used when naming variables, functions, object properties and others. They give programmers an easy opportunity to name different data structures and use them in various language structures.
In JavaScript identifiers may be used for any of the following:
In JavaScript identifiers may be used for any of the following:
- Variable names
- Function names
- Names of formal parameters of functions
- As a part of label structures
- Object property names
JavaScript identifier must begin with a letter, an underscore (_), or a dollar sign ($). Subsequent characters can be letters, digits, underscores, or dollar signs. (Digits are not allowed as the first character so that JavaScript can easily distinguish identifiers from numbers.)
Other than sticking to the rules of naming an identifier, you also need to keep in mind that you cannot use the reserved keywords in JavaScript for naming your identifier in the program.
The standard that defines JavaScript (aka JScript) is the third edition of The ECMAScript Programming Language.
ECMAScript 3 reserved all the keywords of the Java language. If you plan to run your code under an ECMAScript 3 implementation of JavaScript then you must avoid using the following reserved keywords:
The standard that defines JavaScript (aka JScript) is the third edition of The ECMAScript Programming Language.
ECMAScript 3 reserved all the keywords of the Java language. If you plan to run your code under an ECMAScript 3 implementation of JavaScript then you must avoid using the following reserved keywords:
abstract | boolean | break | byte |
char | class | const | continue |
default | debugger | delete | do |
double | else | enum | export |
extends | false | final | finally |
float | for | function | goto |
if | implements | import | in |
instanceof | int | interface | long |
native | new | null | package |
private | protected | public | return |
short | static | super | switch |
synchronized | this | throw | throws |
transient | true | try | typeOf |
var | volatile | void | while |
with |
Although this has been relaxed in ECMAScript 5, you should still avoid all of these identifiers. Most of the reserved words in this list are not used in the language.
Note that the above list does not include the words like undefined, NaN, Infinity, Array, Number, Boolean, Date, Error, isNan, parseInt, parseFloat, String, SyntaxError, TypeError, eval, JSON etc.
But it is advised to avoid these as well while naming an identifier.
Note that the above list does not include the words like undefined, NaN, Infinity, Array, Number, Boolean, Date, Error, isNan, parseInt, parseFloat, String, SyntaxError, TypeError, eval, JSON etc.
But it is advised to avoid these as well while naming an identifier.
References:
Mozilla Developer's network
Javascript: The Definitive Guide by David Flanagan (6th edition, O'Reilly Media,Inc.)
Mozilla Developer's network
Javascript: The Definitive Guide by David Flanagan (6th edition, O'Reilly Media,Inc.)