JavaScript identifiers are the name that we give to variables, objects, functions, arrays, classes, etc. We must use a unique name so as to identify them. We then use the identifier to refer to the variable, functions, etc elsewhere in the program. There are certain rules & restrictions that we must follow when we name an identifier.
Table of Contents
Example of Identifier
In the example below, the message is the name we have given to the variable. The variable holds the string “hello world”. Hence the message is Identifier.
1 2 3 4 | var message; message=”hello world”; |
In the following example, sayHello
is the identifier.
1 2 3 4 5 | function sayHello(){ console.log('Hi') } |
JavaScript Identifiers Rules
When you name any identifier, you need to follow these rules.
- The identifier name must be unique within the scope.
- The first letter of an identifier should be a
- upper case letter
- Lower case letter
- underscore
- dollar sign
- The subsequent letters of an identifier can have
- upper case letter
- Lower case letter
- underscore
- dollar sign
- numeric digit
- We cannot use reserved keywords. You can find the list of keywords for here.
- They are case-sensitive. For Example,
sayHello
is different fromSayHello
- Cannot use spaces in a identifier name.
- Avoid using the JavaScript Reserved Keywords as identifier name
Example of Valid Identifiers
Valid Identifier Names | ||
---|---|---|
empName | emp_name | _empName |
result1 | $result |
Example of Invalid Identifiers
identifier | Reason |
---|---|
break | Because it is a Reserved keyword |
emp name | spaces are not allowed |
emp-name | – not allowed |
1result | cannot begin with a digit |
emp@name | @ not allowed. only underscore and dollar allowed |