JavaScript JS Tutorial JS Objects JS Functions JS Classes JS Async JS HTML DOM JS Browser BOM JS Web APIs JS AJAX JS JSON JS vs jQuery JS Graphics



JS Reserved Words

JavaScript is a popular programming language used for creating interactive web pages. It has a set of reserved words that have a special meaning in the language and cannot be used as variable names or function names. In this article, we will discuss the JS reserved words and their usage.

Brief Explanation of JS Reserved Words

JS reserved words are the words that have a predefined meaning in the JavaScript language. These words cannot be used as variable names or function names. If you try to use a reserved word as a variable name or function name, you will get a syntax error.

JS reserved words are used to define the syntax and structure of the language. They are used to create loops, conditions, functions, and other programming constructs. Some of the most commonly used JS reserved words are:

  • break
  • case
  • catch
  • const
  • continue
  • debugger
  • default
  • delete
  • do
  • else
  • false
  • finally
  • for
  • function
  • if
  • in
  • instanceof
  • new
  • null
  • return
  • switch
  • this
  • throw
  • true
  • try
  • typeof
  • var
  • void
  • while
  • with

Let's take a look at some examples of how these reserved words are used in JavaScript:

Example 1: if statement

The if statement is used to execute a block of code if a condition is true. Here's an example:


if (x > 10) {
  console.log("x is greater than 10");
}

In this example, the reserved word if is used to define the condition that needs to be checked. If the condition is true, the code inside the curly braces will be executed.

Example 2: for loop

The for loop is used to execute a block of code a certain number of times. Here's an example:


for (var i = 0; i < 10; i++) {
  console.log(i);
}

In this example, the reserved word for is used to define the loop. The loop will execute as long as the condition inside the parentheses is true. The variable i is initialized to 0, and it will be incremented by 1 after each iteration of the loop.

Example 3: function declaration

The function reserved word is used to declare a function in JavaScript. Here's an example:


function greet(name) {
  console.log("Hello, " + name + "!");
}

In this example, the reserved word function is used to declare a function called greet. The function takes one parameter called name, and it logs a greeting message to the console.

Conclusion

JS reserved words are an important part of the JavaScript language. They have a predefined meaning and cannot be used as variable names or function names. Understanding the usage of these reserved words is essential for writing efficient and error-free JavaScript code.

References

Activity