Table of contents
1.
Introduction
2.
Example of Identifier
3.
JavaScript Identifiers Rules
4.
Example of Valid Identifiers
4.1.
Example of Invalid Identifiers
5.
Frequently Asked Questions
5.1.
What are JavaScript identifiers used for?
5.2.
Can an identifier start with a number in JavaScript?
5.3.
Why are JavaScript identifiers case-sensitive?
6.
Conclusion
Last Updated: Feb 23, 2025
Easy

What are JavaScript Identifiers?

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In JavaScript, identifiers are the names used to identify variables, functions, and objects. They must start with a letter, an underscore (_), or a dollar sign ($) and can be followed by letters, digits, or underscores. Identifiers are case-sensitive and cannot be JavaScript reserved keywords.

What are JavaScript Identifiers?

In this article, you will learn about JavaScript identifiers, their rules, best practices for naming variables, and examples of valid and invalid identifiers.

Example of Identifier

An identifier is simply the name of a variable, function, or object in JavaScript. Here’s an example of JavaScript identifiers:

// Variable Identifiers
let studentName = "John";
let age = 21;

// Function Identifier
function calculateSum(a, b) {
    return a + b;
}

// Object Identifier
let person = {
    firstName: "Alice",
    lastName: "Brown"
};

console.log(studentName);  
console.log(age);         
console.log(calculateSum(5, 10)); 
console.log(person.firstName);   
You can also try this code with Online Javascript Compiler
Run Code

 

Output

John
21
15
Alice 

 

Explanation:

  1. studentName and age are variable identifiers.
     
  2. calculateSum is a function identifier.
     
  3. person is an object identifier, and firstName & lastName are property identifiers.

JavaScript Identifiers Rules

When creating an identifier in JavaScript, you must follow specific rules:

  1. Identifiers must begin with a letter (A-Z or a-z), an underscore (_) or a dollar sign ($).
     
  2. The following characters can be letters, digits (0-9), underscores, or dollar signs.
     
  3. JavaScript identifiers are case-sensitive.
     
  4. They cannot be the same as JavaScript reserved keywords like var, function, return, etc.

Example of Valid Identifiers

The following identifiers follow JavaScript naming rules and will work correctly:

let name = "Alex";
let _age = 25;
let $salary = 5000;
let user123 = "Mark";
function myFunction() {
    return "Hello, World!";
}
You can also try this code with Online Javascript Compiler
Run Code

 

Explanation:

  1. name starts with a letter (valid).
     
  2. _age starts with an underscore (valid).
     
  3. $salary starts with a dollar sign (valid).
     
  4. user123 includes letters and numbers after the first character (valid).
     
  5. myFunction follows standard naming conventions for functions.

Example of Invalid Identifiers

The following identifiers do not follow JavaScript naming rules and will result in errors:

let 123user = "Tom";  // Invalid: Starts with a number
let var = 10;          // Invalid: Uses a reserved keyword
let my-name = "Anna"; // Invalid: Contains a hyphen (-)
let first name = "Sam"; // Invalid: Contains a space
You can also try this code with Online Javascript Compiler
Run Code

 

Explanation:

  1. 123user starts with a number, which is not allowed.
     
  2. var is a reserved keyword, so it cannot be used as an identifier.
     
  3. my-name contains a hyphen, which is not permitted.
     
  4. first name has a space, making it invalid.

Frequently Asked Questions

What are JavaScript identifiers used for?

JavaScript identifiers are used to name variables, functions, objects, and properties for easy reference and data management.

Can an identifier start with a number in JavaScript?

No, identifiers cannot start with a number. They must begin with a letter, underscore, or dollar sign.

Why are JavaScript identifiers case-sensitive?

JavaScript differentiates between uppercase and lowercase letters, so myVar and myvar are considered different identifiers.

Conclusion

In JavaScript, identifiers play a crucial role in naming variables, functions, and objects. Following proper naming rules ensures your code runs smoothly without syntax errors. Identifiers should begin with a letter, underscore, or dollar sign, and must not use JavaScript reserved keywords.

Live masterclass