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
How to name JavaScript Identifiers?
In this tutorial, we will learn how to name JavaScript Identifiers.
Identifiers in JavaScript are the names we give to variables, arrays, objects, functions, etc. We must give them unique names to identify them properly. There are specific rules we must follow when naming identifiers that are common to most programming languages.
Rules for Naming Identifiers
There are certain rules we have to follow before naming an identifier. A proper name helps the programmer to make the code more effective. The rules are the following:
JavaScript identifier names should not start with any numeric values like 0-9. For example,
0xyzand87bare invalid names.Identifier names should start with an alphabet, dollar sign (
$), or underscore character. For example,Abc9,_abc,$pqare valid.Excluding the first character, the rest of the name can include letters, numbers, dollar sign (
$), or underscore. But we cannot include any special characters including space (#,@," ") other than underscore.JavaScript Identifier names are case sensitive: for example
Abc,ABC,aBc,abcthese all are the names of different variables.There are some reserved keywords in JavaScript. Those are called reserved words. We cannot choose these keywords to make an identifier name. This makes the compiler confused. For example:
break,let,new,boolean, etc. are not valid variable names.
Reserved Keywords
The list of reserved keywords in JavaScript are:
| abstract | arguments | await | boolean |
| break | byte | case | catch |
| char | class | const | continue |
| debugger | default | delete | do |
| double | else | enum | eval |
| export | extends | false | final |
| finally | float | for | function |
| goto | if | implements | import |
| in | instanceof | int | interface |
| let | long | native | new |
| null | package | private | protected |
| public | return | short | static |
| super | switch | synchronized | this |
| throw | throws | transient | true |
| try | typeof | var | void |
| volatile | while | with | yield |
Best Practices for Naming Identifiers
Make the Identifier names descriptive. In a long code using names with one character (like
a,b, etc) could not help you to remember what this variable is used for. But too lengthy names are inefficient. Up to 20 characters with 2 to 4 words are enough to make a decent identifier name.Use multiple words to name an Identifier to make it descriptive.
Blank spaces are not allowed in identifier names. We can name them Camel case (like
firstName) or use underscore (likefirst_name) to make it more readable.As JavaScript Identifiers are case sensitive make sure you did not make multiple variables with the same name but different cases (upper or lower). That makes the programmer confused and ends up with errors.
Syntax
// Assigning value to a variable
let identifier_name = value;
// Declaring a function
function identifier_name() {
// statement
}
In the above syntax, we used two kinds of identifiers - variable and function. To assign a value first, we declare the variable name with the let keyword. Then we assign the value using assignment operator. And in the function, we name a function and put the lines of code inside the function.
Example: Variable Identifiers
In the below example, we will learn about how to name variable identifiers and initialize them with values, and access them.
<html>
<body>
<h3> Showing different types of <i> variable Identifier values </i> </h3>
<div id="root"> </div>
<script>
// initializing variables with values
let number = 5; // variable to store number
let string = "Hello World"; // variable to store string
let bool = true; // variable to store boolean
document.getElementById('root').innerHTML = "number: " + number +
"<br/>" + "string: " + string + "<br/>" + "boolean: " + bool;
</script>
</body>
</html>
number: 5 string: Hello World boolean: true
Example: Array and Object Identifiers
In the below example, we will learn about how to name array and object identifiers and initialize them with values. For showing the object values as a string we used the JSON.stringify() method.
<html>
<body>
<h3> Showing <i> array and object Identifier values </i> </h3>
<div id="root"> </div>
<script>
let arr = ["apple", "banana", "mango"]; // array identifier
let obj = { productName: "book", price: 200 }; // object identifier
document.getElementById('root').innerHTML = "array: " + arr + "<br/>" +
"object: " + JSON.stringify(obj);
</script>
</body>
</html>
array: apple,banana,mango
object: {"productName":"book","price":200}
Conclusion
JavaScript identifiers must follow specific naming rules and cannot use reserved keywords. Use descriptive names with camelCase or underscore notation for better code readability and maintainability.
