JavaScript Identifiers

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.

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.

In the following example, sayHello is the identifier.

JavaScript Identifiers Rules

When you name any identifier, you need to follow these rules.

  1. The identifier name must be unique within the scope.
  2. The first letter of an identifier should be a
    1. upper case letter
    2. Lower case letter
    3. underscore
    4. dollar sign
  3. The subsequent letters of an identifier can have
    1. upper case letter
    2. Lower case letter
    3. underscore
    4. dollar sign
    5. numeric digit
  4. We cannot use reserved keywords. You can find the list of keywords for here.
  5. They are case-sensitive. For Example, sayHello is different from SayHello
  6. Cannot use spaces in a identifier name.
  7. Avoid using the JavaScript Reserved Keywords as identifier name

Example of Valid Identifiers

Valid Identifier Names
empNameemp_name_empName
result1$result

Example of Invalid Identifiers

identifierReason
breakBecause it is a Reserved keyword
emp namespaces are not allowed
emp-name– not allowed
1resultcannot begin with a digit
[email protected]@ not allowed. only underscore
and dollar allowed

References

Identifier

Leave a Comment

Your email address will not be published.

Scroll to Top