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.
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.
var message;
message=”hello world”
In the following example, sayHello is the identifier.
function sayHello(){
console.log('Hi')
}
When you name any identifier, you need to follow these rules.
| Valid Identifier Names | ||
|---|---|---|
| empName | emp_name | _empName |
| result1 | $result | |
| 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 | |