- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Identifiers
JavaScript Statement & Expression
JavaScript Identifiers
JavaScript identifiers are names used to identify variables, functions, objects, classes, and other items in code.
Example
Example
javascript
let userName = "Amit";Here, userName is an identifier.
What are Identifiers?
An identifier is simply a name given to something in JavaScript.
You use identifiers to name:
| Item | Example |
|---|---|
| Variable | let age = 25; |
| Function | function showMessage() {} |
| Object | let student = {}; |
| Class | class User {} |
Example
javascript
let courseName = "JavaScript";
function showCourse() {
console.log(courseName);
}In this example, courseName and showCourse are identifiers.
Rules for JavaScript Identifiers
JavaScript identifiers must follow some rules.
| Rule | Valid Example | Invalid Example |
|---|---|---|
| Can contain letters | name | |
| Can contain digits, but not at the beginning | name1 | 1name |
Can contain underscore _ | user_name | |
Can contain dollar sign $ | $price | |
| Cannot contain spaces | userName | user name |
| Cannot be a JavaScript keyword | userAge | let |
| Are case-sensitive | name, Name |
Valid Identifier Examples
These are valid JavaScript identifiers:
Example
javascript
let name = "Amit";
let userName = "Rahul";
let user_age = 25;
let $price = 500;
let totalAmount1 = 1000;All these names follow JavaScript identifier rules.
Invalid Identifier Examples
These are invalid JavaScript identifiers:
Example
javascript
let 1name = "Amit";
let user name = "Rahul";
let user-name = "Amit";
let let = "JavaScript";These are invalid because:
| Identifier | Problem |
|---|---|
1name | Starts with a number |
user name | Contains a space |
user-name | Hyphen is not allowed |
let | JavaScript keyword |
Identifiers are Case-Sensitive
JavaScript identifiers are case-sensitive.
This means name, Name, and NAME are different identifiers.
Example
javascript
let name = "Amit";
let Name = "Rahul";
let NAME = "Suman";JavaScript treats all three as different variables.
Good Identifier Names
A good identifier should clearly describe what it stores or does.
Good examples:
Example
javascript
let studentName = "Amit";
let totalPrice = 1500;
let isLoggedIn = true;
function calculateTotal() {
// code here
}These names are easy to understand.
Poor Identifier Names
Avoid unclear names when writing beginner-friendly or real project code.
Example:
Example
javascript
let x = "Amit";
let y = 1500;
let z = true;These names work, but they do not clearly explain what the values mean.
A better version:
Example
javascript
let studentName = "Amit";
let totalPrice = 1500;
let isLoggedIn = true;camelCase Naming
In JavaScript, camelCase is commonly used for variable and function names.
In camelCase, the first word starts with a lowercase letter, and each next word starts with an uppercase letter.
Example
javascript
let firstName = "Amit";
let totalPrice = 500;
let userEmailAddress = "amit@example.com";
function showUserName() {
console.log(firstName);
}camelCase makes names readable and clean.
Identifiers with Functions
Function names are also identifiers.
Example
javascript
function showMessage() {
console.log("Hello JavaScript");
}Here, showMessage is an identifier.
Function names should describe the action they perform.
Good examples:
Example
javascript
function calculateTotal() {}
function showAlert() {}
function validateForm() {}
function getUserName() {}Identifiers with Objects
Object property names can also be identifiers.
Example
javascript
let student = {
name: "Amit",
age: 20,
course: "JavaScript"
};Here, student, name, age, and course are identifiers.
You can access object values like this:
Example
javascript
console.log(student.name);
console.log(student.course);Complete Example
Complete Example
html
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Identifiers</title>
</head>
<body>
<h1>JavaScript Identifiers</h1>
<p id="result"></p>
<script>
let studentName = "Amit";
let courseName = "JavaScript";
let courseFee = 999;
function showDetails() {
document.getElementById("result").innerHTML =
studentName + " is learning " + courseName +
". Course fee is " + courseFee + ".";
}
showDetails();
</script>
</body>
</html>Try It Yourself
Run the above example in the Try It Editor.
Try changing the identifier names such as studentName, courseName, and showDetails.
Make sure the new names follow JavaScript identifier rules.
Important Points
- Identifiers are names used for variables, functions, objects, classes, and more.
- Identifiers can contain letters, digits, underscore, and dollar sign.
- Identifiers cannot start with a number.
- Identifiers cannot contain spaces.
- Identifiers cannot be JavaScript keywords.
- JavaScript identifiers are case-sensitive.
- camelCase is commonly used for variable and function names in JavaScript.
Conclusion
JavaScript identifiers are names used to identify different parts of a program.
Good identifiers make code easier to read and understand.
When writing JavaScript, always use meaningful names that follow identifier rules and clearly describe the purpose of the variable, function, or object.
