Identifiers and Reserved Words in JavaScript
In the Literals in JavaScript blog, we have learned about the Literals. In JavaScript, literals are like straightforward shortcuts for expressing values directly in code, such as numbers or text. They serve as constants that stay fixed throughout the program. In this blog, we will learn about Identifiers and Reserved Words in JavaScript.
What are Identifiers?
The name itself indicates that it identifies the memory location. The names given to the memory locations are called Identifiers. For example, variables can be identifiers. The variables that we use in programs are identifiers. Examples of Identifiers are variables, function names, constants, etc.
Rules for Identifiers
- The identifier must begin with a letter, an underscore (_), or a dollar symbol ($), followed by the letters, digits, underscores, or dollar signs.
- The identifier's name should not begin with numbers or any special symbols except for underscore (_) and dollar sign ($).
- The identifier name cannot be a keyword. For example, if, else, while, for, etc cannot be used as an identifier name.
- Identifier names are case-sensitive. For example, name, NAME, Name are treated as different variables. Even though the names given are the same, the casing differs.
Valid Identifiers
Let’s see with the help of example Valid Identifiers
Invalid Identifiers
Let’s take an example of the identifiers that are invalid
Reserved Words
In JavaScript, reserved words, also called keywords, have specific meanings reserved for special purposes in the language. Due to their crucial roles in the language's structure and operation, these words cannot be used as identifiers, like variable or function names. Trying to use reserved words as identifiers will cause syntax problems. Here is a list of words that you can't use as identifiers:
Reserved Word | Description |
---|---|
break | Terminates a loop or switch statement |
case | Defines a specific case in a switch statement |
catch | Catches exceptions thrown in try blocks |
class | Declares a class in JavaScript |
const | Declares a constant variable |
continue | Skips the rest of a loop and continues with the next iteration |
debugger | Pauses the execution of JavaScript code |
default | Defines the default case in a switch statement |
delete | Deletes a property from an object |
do | Starts a do...while loop |
else | Defines an alternative block of code in an if statement |
export | Exports a module or a part of it for use in other scripts |
extends | Extends a class to create a new class |
false | Represents the Boolean value false |
finally | Specifies a block of code to be executed after a try block |
for | Initiates a for loop |
function | Declares a function |
if | Creates a conditional statement |
import | Imports modules or specific members from them |
in | Checks if a specified property is in an object |
instanceof | Checks if an object is an instance of a specific class |
new | Creates an instance of a user-defined object type |
null | Represents the absence of a value or a null object |
return | Exits a function and specifies a value to be returned |
super | Calls the parent constructor in an extended class |
switch | Evaluates an expression and executes the corresponding case |
this | Refers to the current object |
throw | Throws an exception |
true | Represents the Boolean value true |
try | Encloses a block of code that might throw an exception |
typeof | Returns a string indicating the type of an operand |
undefined | Represents an undefined value |
var | Declares a variable |
void | Evaluates an expression and returns undefined |
while | Creates a while loop |
with | Extends the scope chain for a statement |
yield | Pauses and resumes a generator function |
Semicolon
In JavaScript, we use semicolons (;) to separate statements. If statements are on separate lines, we can skip the semicolon between them. For instance:
Here, since the statements are on different lines, we can omit the semicolon in the first statement. However, if statements are on the same line, like:
We cannot omit the semicolon, as both statements are on the same line.
JavaScript code involves choosing unique identifiers, steering clear of reserved words like "if" or "function," and punctuating statements with semicolons for flawless execution.