Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are JavaScript Identifiers?
JavaScript identifiers are names given to variables, functions, classes, objects, and other entities in your code. They work similarly to identifiers in other programming languages like C, C++, and Java, but have specific rules you must follow.
What are Identifiers?
An identifier is simply a name that you create to reference a variable, function, or other code element. Here are some examples of valid identifiers:
// Valid variable identifiers let userName = "John"; let age = 25; let _privateVar = "hidden"; let $element = "jQuery style"; let camelCaseVariable = "standard naming"; console.log(userName); console.log(age); console.log(_privateVar);
John 25 hidden
Naming Rules
JavaScript identifiers must follow these strict rules:
1. Cannot Start with Numbers
// Invalid - starts with number // let 5demo = "invalid"; // Valid - starts with letter or underscore let demo5 = "valid"; let _5demo = "also valid"; console.log(demo5); console.log(_5demo);
valid also valid
2. Case Sensitivity
let Name = "Alice";
let name = "Bob";
let NAME = "Charlie";
console.log("Name:", Name);
console.log("name:", name);
console.log("NAME:", NAME);
Name: Alice name: Bob NAME: Charlie
3. Reserved Keywords
You cannot use JavaScript reserved keywords as identifiers. Here are some common ones:
// Invalid identifiers (reserved keywords) let break = "invalid"; // Error let function = "invalid"; // Error let return = "invalid"; // Error let class = "invalid"; // Error // Valid alternatives let breakPoint = "valid"; let functionName = "valid"; let returnValue = "valid"; let className = "valid";
Valid Characters
Identifiers can contain:
// Letters (a-z, A-Z) let firstName = "John"; // Numbers (but not as first character) let user123 = "valid"; // Underscore (_) let _private = "underscore start"; let my_variable = "underscore middle"; // Dollar sign ($) let $jquery = "dollar start"; let price$ = "dollar end"; console.log(firstName, user123, _private, $jquery);
John valid underscore start dollar start
Best Practices
Follow these conventions for readable code:
// Use camelCase for variables and functions
let userAge = 25;
let calculateTotal = function() { return 100; };
// Use PascalCase for constructors and classes
function UserAccount() { this.name = "test"; }
// Use UPPER_CASE for constants
const MAX_USERS = 100;
const API_URL = "https://api.example.com";
console.log("Age:", userAge);
console.log("Max users:", MAX_USERS);
Age: 25 Max users: 100
Common Examples
| Identifier Type | Valid Examples | Invalid Examples |
|---|---|---|
| Variables |
userName, _age, $element
|
5user, user-name, break
|
| Functions |
calculateTotal, getData
|
calculate-total, function
|
| Constants |
MAX_VALUE, API_KEY
|
2MAX, const
|
Conclusion
JavaScript identifiers must start with a letter, underscore, or dollar sign, followed by any combination of letters, numbers, underscores, or dollar signs. They are case-sensitive and cannot be reserved keywords. Following proper naming conventions makes your code more readable and maintainable.
