codedamn logo
codedamn

What are keywords in JavaScript?

  • Vishnupriya's profile image
    Vishnupriya
    Team codedamn
What are keywords in JavaScript?

Keywords in JavaScript

Keywords in JavaScript are a set of reserved words that cannot be used as names of functions, labels, or variables as they are already a part of the syntax of JavaScript. Each of the keywords has its own meaning. They are generally used in executing internal operations. Some of the keywords are listed below.

List of Keywords

while case class void function instanceof
throw export delete catch private package
true  debugger extends default interface super
with enum if return switch try
let yield typeof public for static
new else finally false import var
do protected null in implements this
await const continue break

Now let us discuss some of the keywords in detail to understand what they are and how we can use them.

let

The let keyword enables us to declare a variable, we can also assign a value to the variable but this is completely optional. 

Given below is an example of usage of the let keyword:

let num = 1;
if (num === 1) {
  let num = 2;
  console.log(num);
  // expected output: 2
}
console.log(num);
// expected output: 1

const

This keyword resembles the let keyword but the difference between the two keywords is that we can’t change the value of the variable once assigned using the const keyword. It can neither be changed with reassignment nor with redeclaration. Let us understand this with the example given below:

const x = 4;
console.log(x);

When you run the above-written code, you will get the output as shown below:

4

Now let us try to change the value of the variable “x”.

const x = 4;

try {
  x = 44;
} catch (err) {
  console.log(err);
  // TypeError: Assignment to constant variable.
}

When you run the above-written code, you will get the output as shown below:

> TypeError: invalid assignment to const `number'

While

This keyword is used in creating a loop that gives output to a particular statement until the specified condition returns true. The test condition is evaluated before passing into the loop. The statement executes if the condition is true and when the condition is false, it runs to the next lines of code written below. Let us understand the working of the while loop with an example.

let num = 0;

while (num < 3) {
  num++;
}
console.log(num);

When you run the above-written code, you will get the output as shown below:

3

for

This keyword is used in creating loops using a statement that generally consists of three expressions followed by a block of code. The three expressions are generally enclosed in parentheses set apart using semicolons. Let us understand the working of the for loop with an example.

let num = '';

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

You are going to get the output as shown below:

“01234”

switch

It is used in performing actions based on conditions. We can select one of the code blocks with it. Switch evaluates the statement and prints the output associated with the statement when there is a match. The default code block is printed when there is no match. Let us understand the working of the switch statement with an example:

const month = 'february';

switch (month) {
  case 'march':
    console.log('March has 31 days.');
    break;

  case 'february':
    console.log('February has 28 days.');
    break;

  case 'april':
    console.log('April has 30 days.');
    break;

  default:
    console.log(`Sorry, we are out of ${month}.`);
}

When you run the above-written code, you will get the output as shown below:

“February has 28 days”

export

This statement is used to export live bindings (a reference to the location where the exported values are stored) to objects, functions, etc so that we can use the module by using an import statement. Let us understand the export statement using an example:

// module “newone.js”

export default function square(x) {
    return x * x * x;
}

After exporting, we can use this module in a different script just by using import statement as shown below.

import cube from ‘./newone.js’;

console.log(square(5))

You are going to get the output as shown below.

25

break

The break statement is used in terminating the present loop and start executing the lines of code present below it. We can jump out of a loop using this statement. Let us understand the break statement using an example:

let i = 0;

while (i < 4) {
  if (i === 3) {
    break;
  }
  i = i + 1;
}
console.log(i);

When you run the above-written code, you will get the output as shown below:

3

return

This statement ends the execution of the function and prints a specific value of the function. Let us understand the break statement using an example:

var x = myFunction(6,7) ;       

function myFunction(a, b) {
 return a * b;              
}
console.log(x);

When you run the above-written code, you will get the output as shown below:

42

continue

This statement stops the execution of commands of the present loop and continues to execute the next lines of code present below the continue statement. Let us understand the continue statement using an example:

let num = '';

for (let i = 2; i < 6; i++) {
  if (i === 4) {
    continue;
  }
  num = num + i;
}
console.log(num);

When you run the above-written code, you will get the output as shown below:

235

yield

This statement is used to pause and resume a function that returns a generator object. It does it asynchronously. Such a function is known as a generator function. The only difference between the generator function and the normal function is that the generator function returns the output using the yield keyword. This keyword eliminates all the problems of nested callback. By using yield, we generally get two outputs, one is the value and the other is a boolean. Let us understand how a yield statement is used to return a value:

function* foo() { 
    yield 1;
    yield 2;
    yield 3;
}

let f = foo();
console.log(f.next());

When you run the above-written code, you will get the output as shown below:

{ value: 1, done: false }

Now let us see how yield is used to return undefined

function* bar() {
    yield;
}

let b = bar();
console.log(b.next()); 

You will get the output as shown below:

{ value: undefined, done: false }

Do check out our blog on Five Types Of Errors In JavaScript – Method, Messages & Fixation Steps 

You can also check our courses on Codedamn.

User avatar