JavaScript Reserved Words

Reserved words, also known as keywords, are predefined terms in JavaScript that serve specific purposes and cannot be used as identifiers (such as variable names or function names).

These words are reserved for use by the JavaScript language itself and cannot be redefined or used for other purposes within a script.

Let's explore reserved words in JavaScript in more detail:

Importance of Reserved Words

Here are some examples list of reserved words in JavaScript:

Reserved Word Description
abstract Reserved for possible future use in ECMAScript.
await Used to pause the execution of an async function until a promise is settled.
boolean Specifies a Boolean value: true or false.
break Terminates the current loop, switch, or label statement.
byte Represents an 8-bit integer value.
case Marks a block of statements to be executed in a switch statement.
catch Specifies a block of code to be executed when an error occurs in a try block.
char Represents a character (16-bit Unicode character).
class Declares a class.
const Declares a constant variable.
continue Jumps to the next iteration of a loop.
debugger Stops the execution of JavaScript, and calls (if available) the debugging function.
default Specifies the default case in a switch statement.
delete Deletes an object's property.
do Marks a block of statements to be executed while a condition is true.
double Represents a 64-bit floating-point number.
else Specifies a block of code to be executed if a condition is false.
enum Declares an enumeration.
export Used to export functions, objects, or primitives from a file.
extends Extends a class (inheritance).
false Represents the boolean value false.
final Reserved for possible future use in ECMAScript.
finally Specifies a block of code to be executed after a try block.
float Represents a 32-bit floating-point number.
for Marks a block of statements to be executed repeatedly.
function Declares a function.
goto Reserved for possible future use in ECMAScript.
if Marks a block of statements to be executed if a condition is true.
implements Implements an interface.
import Used to import functions, objects, or primitives into a file.
in Specifies the property name or array index.
instanceof Determines whether an object is an instance of a specified object.
int Represents a 32-bit integer.
interface Declares an interface.
let Declares a block-scoped local variable.
long Represents a 64-bit integer.
native Declares a native method.
new Creates an instance of a constructor function.
null Represents the null value.
package Declares a package.
private Declares a private method or field.
protected Declares a protected method or field.
public Declares a public method or field.
return Specifies the return value of a function.
short Represents a 16-bit integer.
static Defines a static method or property.
super Refers to the parent class.
switch Marks a block of statements to be executed based on different cases.
synchronized Reserved for possible future use in ECMAScript.
this Refers to the current object.
throw Throws an error.
throws Specifies the exceptions that a method can throw.
transient Reserved for possible future use in ECMAScript.
true Represents the boolean value true.
try Marks a block of statements to be executed and handles exceptions.
typeof Returns the type of a variable or expression.
undefined Represents an undefined value.
var Declares a variable.
void Specifies that a function does not return a value.
volatile Reserved for possible future use in ECMAScript.
while Marks a block of statements to be executed while a condition is true.
with Extends the scope chain for a statement.
yield Pauses and resumes a generator function.
// Examples of using reserved words in JavaScript
let x = 10; // 'let' is a reserved word used for variable declaration
function add(name){ // 'function' is a reserved word used to declare functions
  return "Hello, " + name + "!"; // 'return' is a reserved word used to return a value from a function
}

if (x > 5){ // 'if' is a reserved word used in conditional statements
  console.log("x is greater than 5");
}else{
  console.log("x is less than or equal to 5");
}

In the above example,we use several reserved words such as var, function, if, returnand console.log()to demonstrate their usage in JavaScript code.

Example

// Examples of using reserved words in JavaScript
let x = 10; // 'let' is a reserved word used for variable declaration
function add(name){ // 'function' is a reserved word used to declare functions
  return "Hello, " + name + "!"; // 'return' is a reserved word used to return a value from a function
}
console.log(add("Bilal"));

if (x > 5){ // 'if' is a reserved word used in conditional statements
  console.log("x is greater than 5");
}else{
  console.log("x is less than or equal to 5");
}

Avoiding Conflicts

It's important to avoid using reserved words as identifiers in JavaScript code to prevent syntax errors and unexpected behavior.

Attempting to use a reserved word as a variable name or function name will result in a syntax error.

// Attempting to use a reserved word as a variable name
let return  = "Hello"; // SyntaxError: Unexpected token 'return'

Example


// Attempting to use a reserved word as a variable name
let return  = "Hello"; // SyntaxError: Unexpected token 'return'
console.log(return);

By adhering to the rules regarding reserved words, developers can write clean and readable code that conforms to JavaScript language standards.

Reserved words are predefined terms in JavaScript that serve specific purposes and cannot be used as identifiers.

They play a crucial role in defining the syntax and structure of JavaScript code, ensuring consistency and preventing conflicts with user-defined names.