Bite-sized JavaScript: Reserved Words

In JavaScript there are certain words and phrases that are reserved for specific purposes. These reserved words, also known as keywords, cannot be used as variable names or identifiers in a program.

Bite-sized JavaScript: Reserved Words
Photo by Dmitri Nesteruk / Unsplash

In JavaScript there are certain words and phrases that are reserved for specific purposes. These reserved words, also known as keywords, cannot be used as variable names or identifiers in a program. In this blog, we will discuss the most commonly used JavaScript reserved words and their meaning, along with examples.

break

The break keyword is used to terminate a loop or switch statement. When encountered, it exits the current loop or switch statement and continues execution of the program after the loop or switch.

Example:

for (let i = 0; i < 10; i++) {
  if (i === 5) {
    break;
  }
  console.log(i);
}

In this example, the for loop runs 10 times, but when i is equal to 5, the break statement is encountered and the loop is terminated.

case

The case keyword is used in a switch statement to define a set of actions to be taken based on a particular value.

Example:

let fruit = "apple";
switch (fruit) {
  case "apple":
    console.log("You selected apple");
    break;
  case "banana":
    console.log("You selected banana");
    break;
  default:
    console.log("Invalid selection");
}

In this example, the switch statement checks the value of the fruit variable and executes the appropriate case statement.

const

The const keyword is used to declare a variable with a constant value that cannot be reassigned.

Example:

const PI = 3.14;
console.log(PI); // 3.14
PI = 3.14159; // TypeError: Assignment to constant variable.

In this example, the const keyword is used to declare a constant variable PI with the value of 3.14. When an attempt is made to reassign the value of PI, a TypeError is thrown.

continue

The continue keyword is used to skip over an iteration of a loop.

Example:

for (let i = 0; i < 10; i++) {
  if (i === 5) {
    continue;
  }
  console.log(i);
}

In this example, the for loop runs 10 times, but when i is equal to 5, the continue statement is encountered and the loop skips that iteration.

else

The else keyword is used in an if statement to define a block of code to be executed if the condition is false.

Example:

let age = 18;
if (age >= 18) {
  console.log("You are old enough to vote");
} else {
  console.log("You are not old enough to vote");
}

In this example, the if statement checks if the age variable is greater than or equal to 18. If it is, the first block of code is executed. Otherwise, the else block of code is executed.

for

The for keyword is used to create a loop that runs a specified number of times.

Example:

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

In this example, the for loop runs 10 times, with the i variable incrementing by 1 on each iteration.

function

The function keyword is used to define a function in JavaScript.

Example:

function addNumbers(a, b) {
  return a + b;
}

console.log(addNumbers(2, 3)); // 5

In this example, the function keyword is used to define a function called addNumbers that takes in two parameters a and b. The function returns the sum of a and b, and it is called with the arguments 2 and 3.

if

The if keyword is used to execute a block of code if a specified condition is true.

Example:

let temperature = 25;

if (temperature > 30) {
  console.log("It's hot outside!");
} else if (temperature > 20) {
  console.log("It's warm outside!");
} else {
  console.log("It's cold outside!");
}

In this example, the if statement checks the value of the temperature variable and executes the appropriate block of code based on the condition.

let

The let keyword is used to declare a variable with a block scope.

Example:

let message = "Hello";
if (true) {
  let message = "World";
  console.log(message); // World
}
console.log(message); // Hello

In this example, the let keyword is used to declare a variable message with a block scope. The variable is declared again inside the if statement with a different value, but it does not affect the value of the outer message variable.

return

The return keyword is used to return a value from a function.

Example:

function multiplyNumbers(a, b) {
  return a * b;
}

console.log(multiplyNumbers(2, 3)); // 6

In this example, the return keyword is used to return the product of a and b from the multiplyNumbers function.

switch

The switch keyword is used to execute different actions based on different conditions.

Example:

let day = "Wednesday";

switch (day) {
  case "Monday":
  case "Tuesday":
  case "Wednesday":
  case "Thursday":
  case "Friday":
    console.log("Weekday");
    break;
  case "Saturday":
  case "Sunday":
    console.log("Weekend");
    break;
  default:
    console.log("Invalid day");
}

In this example, the switch statement checks the value of the day variable and executes the appropriate case statement. If none of the cases match, the default block of code is executed.

var

The var keyword is used to declare a variable with a function scope.

Example:

function printMessage() {
  var message = "Hello";
  console.log(message);
}

printMessage(); // Hello
console.log(message); // ReferenceError: message is not defined

In this example, the var keyword is used to declare a variable message with a function scope. The variable can only be accessed inside the printMessage function.

In conclusion, understanding the meaning and usage of reserved words is important when working with JavaScript. These keywords have specific functions and using them incorrectly can lead to unexpected results or errors in your program. It's important to take note of these reserved words and use them appropriately in your code.

More reserved keywords here: https://www.w3schools.com/js/js_reserved.asp