Identifiers and Reserved Words in JavaScript

Reserved WordsSemicolonInvalid IdentifiersValid IdentifiersRules for Identifiers

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

  1. The identifier must begin with a letter, an underscore (_), or a dollar symbol ($), followed by the letters, digits, underscores, or dollar signs.
  2. The identifier's name should not begin with numbers or any special symbols except for underscore (_) and dollar sign ($).
  3. The identifier name cannot be a keyword. For example, if, else, while, for, etc cannot be used as an identifier name.
  4. 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

let name = "Smith"
let Name = "Smith"        //variables are case sensitive
let firstName = "John"    //combination of characters
let _name="john"          //starts with underscore(_)
let first_name = "john"   //contains underscore(_)
let name_="john"          //ends with underscore(_)
let $name = "john"        //starts with dollar($)
let first$name = "john"   //contains dollar in between of the variable
let name$ = "john"        //ends with dollar($)
let first765name = "john" //contains number
let name869 = "john"      //ends with number

Invalid Identifiers

Let’s take an example of the identifiers that are invalid

let 989abc = "Invalid";           // Identifier cannot start with a digit
let string variable = "Invalid";  // Identifier cannot contain spaces
let while = "Invalid";            // Identifier cannot be a reserved keyword
let string-variable = "Invalid";  // Identifier cannot contain a hyphen
let string@variable = "Invalid";  // Identifier cannot contain the @ symbol
let stringvariable = "Invalid";   // Case matters, so this is a different identifier
let my#variable = "Invalid";      // Contains an invalid character

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 WordDescription
breakTerminates a loop or switch statement
caseDefines a specific case in a switch statement
catchCatches exceptions thrown in try blocks
classDeclares a class in JavaScript
constDeclares a constant variable
continueSkips the rest of a loop and continues with the next iteration
debuggerPauses the execution of JavaScript code
defaultDefines the default case in a switch statement
deleteDeletes a property from an object
doStarts a do...while loop
elseDefines an alternative block of code in an if statement
exportExports a module or a part of it for use in other scripts
extendsExtends a class to create a new class
falseRepresents the Boolean value false
finallySpecifies a block of code to be executed after a try block
forInitiates a for loop
functionDeclares a function
ifCreates a conditional statement
importImports modules or specific members from them
inChecks if a specified property is in an object
instanceofChecks if an object is an instance of a specific class
newCreates an instance of a user-defined object type
nullRepresents the absence of a value or a null object
returnExits a function and specifies a value to be returned
superCalls the parent constructor in an extended class
switchEvaluates an expression and executes the corresponding case
thisRefers to the current object
throwThrows an exception
trueRepresents the Boolean value true
tryEncloses a block of code that might throw an exception
typeofReturns a string indicating the type of an operand
undefinedRepresents an undefined value
varDeclares a variable
voidEvaluates an expression and returns undefined
whileCreates a while loop
withExtends the scope chain for a statement
yieldPauses 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:

x = 10
y = 20;

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:

x=10; y =20;

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.