You are currently viewing JavaScript Cheat Sheet 10.56: JavaScript Reserved Words

JavaScript Cheat Sheet 10.56: JavaScript Reserved Words

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.

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:

  1. break: Terminates the current loop, switch, or label statement.
  2. case: Marks a block of statements to be executed in a switch statement.
  3. catch: Catches exceptions generated by try…catch…finally statements.
  4. class: Defines a class in JavaScript.
  5. const: Declares a constant variable.
  6. continue: Jumps to the next iteration of a loop.
  7. debugger: Stops the execution of JavaScript and opens the debugger.
  8. default: Specifies the default case in a switch statement.
  9. delete: Deletes an object’s property.
  10. do: Starts a do…while loop.
  11. else: Specifies a block of code to be executed if a condition is false.
  12. export: Used to export functions, objects, or primitive values from a module.
  13. extends: Extends a class (used in class declarations or class expressions).
  14. finally: Specifies a block of code to be executed after try and catch blocks.
  15. for: Starts a for loop.
  16. function: Declares a function.
  17. if: Specifies a block of code to be executed if a condition is true.
  18. import: Used to import functions, objects, or primitive values into a module.
  19. in: Checks if a specified property is in an object.
  20. instanceof: Checks if an object is an instance of a specified object type.
  21. new: Creates a new instance of an object.
  22. return: Exits a function and specifies a value to return.
  23. super: Calls the parent constructor.
  24. switch: Evaluates an expression and executes a block of code based on matching cases.
  25. this: Refers to the current object.
  26. throw: Throws an exception.
  27. try: Implements error handling to a block of code.
  28. typeof: Returns the type of a variable or expression.
  29. var: Declares a variable.
  30. void: Specifies that an expression does not return any value.
  31. while: Starts a while loop.
  32. 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:

  1. 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 of i is equal to 3.
  • As a result, the loop stops executing when i becomes 3, and the subsequent iterations are skipped.
  1. 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 named Car.
  • 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 the new keyword and call the displayInfo method to print the car’s information.
  1. Using if and else 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 of x is greater than 0.
  • If the condition evaluates to true, the code block inside the if statement is executed. Otherwise, the code block inside the else statement is executed.
  1. 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 parameters a and b, 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.

Leave a Reply