
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 the names unique to identify them properly. There are some rules we must follow to name the identifiers that are common to most 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, 0xyz, and 87b are invalid names.
Identifier names should start with an alphabet, dollar ($), or underscore character. For example, Abc9, _abc, $pq is a valid one.
Excluding the first character, the rest of the name can include letters, numbers, dollar($), 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, abc these all are the name 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.
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 |
Suggestions to make a good identifier name
Make the Identifier names descriptive. In a long code using names with one word (like a, b, etc) could not help you to remember what is this variable used for. But too much 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 (like first_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; //Assigning value to 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 assigned the value using assigning operator. And in the function, we named a function name the put the line of codes inside the function.
Example 1
In the below example, we will learn about how to name a variable identifier 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>
Example 2
In the below example, we will learn about how to name an array and object identifier and initialize them with values, and access them. For showing the object values as a string we used 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 let obj = { Productname: "book", price: 200 }; //object document.getElementById('root').innerHTML = "array: " + arr + "<br/>" + "object: " + JSON.stringify(obj) </script> </body> </html>
In this tutorial, we learned how can we name JavaScript Identifiers. We learned some basic theories of identifiers. In the two examples, we see used variables, arrays, and object identifiers and output their values in the webpage.