Understanding JavaScript Reserved Words
In JavaScript, reserved words are words that are predefined for specific purposes and cannot be used as identifiers (such as variable names, function names, etc.). These words are reserved for the language syntax and functionality. It’s crucial to understand and recognize these reserved words to avoid syntax errors and unintended behavior in your code. In this tutorial, we’ll cover the basics of JavaScript reserved words, including examples and explanations.
Table of Contents
JavaScript Reserved Words
JavaScript has a set of reserved words that are used for various purposes such as defining control structures, variable declarations, function definitions, and more. Here are some of the common reserved words in JavaScript:
- break: Terminates the current loop, switch, or label statement.
- case: Marks a block of statements to be executed in a switch statement.
- catch: Catches exceptions generated by try…catch…finally statements.
- class: Defines a class in JavaScript.
- const: Declares a constant variable.
- continue: Jumps to the next iteration of a loop.
- debugger: Stops the execution of JavaScript and opens the debugger.
- default: Specifies the default case in a switch statement.
- delete: Deletes an object’s property.
- do: Starts a do…while loop.
- else: Specifies a block of code to be executed if a condition is false.
- export: Used to export functions, objects, or primitive values from a module.
- extends: Extends a class (used in class declarations or class expressions).
- finally: Specifies a block of code to be executed after try and catch blocks.
- for: Starts a for loop.
- function: Declares a function.
- if: Specifies a block of code to be executed if a condition is true.
- import: Used to import functions, objects, or primitive values into a module.
- in: Checks if a specified property is in an object.
- instanceof: Checks if an object is an instance of a specified object type.
- new: Creates a new instance of an object.
- return: Exits a function and specifies a value to return.
- super: Calls the parent constructor.
- switch: Evaluates an expression and executes a block of code based on matching cases.
- this: Refers to the current object.
- throw: Throws an exception.
- try: Implements error handling to a block of code.
- typeof: Returns the type of a variable or expression.
- var: Declares a variable.
- void: Specifies that an expression does not return any value.
- while: Starts a while loop.
- with: Specifies a default object for a block of code.
Example Usage:
Let’s go through some examples to understand how these reserved words are used in JavaScript:
- Using
break
to Terminate a Loop:
for (let i = 0; i < 5; i++) {
if (i === 3) {
break;
}
console.log(i);
}
// Output: 0
// 1
// 2
Explanation:
- In this example, the
break
statement is used to terminate the loop when the value ofi
is equal to 3. - As a result, the loop stops executing when
i
becomes 3, and the subsequent iterations are skipped.
- Using
class
to Define a Class:
class Car {
constructor(make, model) {
this.make = make;
this.model = model;
}
displayInfo() {
console.log(`Car: ${this.make} ${this.model}`);
}
}
const myCar = new Car('Toyota', 'Corolla');
myCar.displayInfo();
// Output: Car: Toyota Corolla
Explanation:
- Here, the
class
keyword is used to define a class namedCar
. - The class has a constructor method to initialize object properties and a method
displayInfo
to display information about the car. - We then create an instance of the
Car
class using thenew
keyword and call thedisplayInfo
method to print the car’s information.
- Using
if
andelse
to Implement Conditional Statements:
let x = 10;
if (x > 0) {
console.log('Positive number');
} else {
console.log('Non-positive number');
}
// Output: Positive number
Explanation:
- In this example, the
if
statement checks if the value ofx
is greater than 0. - If the condition evaluates to true, the code block inside the
if
statement is executed. Otherwise, the code block inside theelse
statement is executed.
- Using
return
to Exit a Function:
function add(a, b) {
return a + b;
}
const result = add(5, 3);
console.log(result);
// Output: 8
Explanation:
- The
return
statement is used to exit the function and specify the value to be returned. - In this example, the
add
function takes two parametersa
andb
, adds them together, and returns the result.
These examples demonstrate how JavaScript reserved words are used in various contexts to control program flow, define structures, and handle data.
Conclusion
Understanding JavaScript reserved words is essential for writing clean, error-free code. By familiarizing yourself with these words and their usage, you’ll be better equipped to leverage JavaScript’s features effectively. Practice using reserved words in different scenarios to reinforce your understanding and improve your programming skills.