JS: Keywords | JavaScript

JavaScript Keywords

JavaScript (JS) keywords (reserved words) has specific meaning while running the code. List of JS keywords & brief usage with examples and the practical use of the syntax. Keywords can not be declared as an identifier (variables, function, class name etc.) in JavaScript. JavaScript throws and error if any keyword used in declaration as an identifier.

JavaScript Keywords are those words used by the JavaScript interpreter have its own meaing. Keywords can not be used as JavaScript variable, function or class names.

var abc = 0; var function = 0; Uncaught SyntaxError: Unexpected token 'function'

class & new

JavaScript class keyword is used to declare a class, according to ECMAScript 6 (ES6) specifications. The new keyword is used to create an object (instance) of a class.

class Car{ } var audi = new Car();

const

const pi = 3.142857142857143;

JavaScript const keyword allows to assign a value on decalraion only.

  • Value cannot be assigned again in a variable declared with const keyword.
  • JavaScript throws error if same variable declared again with const keyword in the same scope.

delete

enum

export

function & return

function pi(){ return 22 / 7; }

JavaScript function keyword is used to declare the reusable piece of code and return a value.

for, in & of

if & else

instanceof

instanceof checks either an identifier is the object of particular class.

var d = new Date(); var b = d instanceof Date; console.log(b); true

d is the object of Date class, so returns true.

var b = 3 instanceof Number; console.log(b); false

Since number is a primitive type, 3 is not an object of Number class, so returns false in this case.

let

JavaScript let keyword is used to declare a variable, recommended to use as compared to var keyword with following merits and demerits:

  • let restricts from re-declare a variable unlike var keyword
  • Variables declared with let keyword is not hoisted as var do
  • let rest of functionality is same as of the var keyword

Please refer Data Types chapter of this tutorial.

null

var v = null;

JavaScript null keyword makes a variable with no value.

super

switch & case

this

this keyword refers the current instance of the class object. this keyword is mostly used to call the class members of the instance to avoid the conflict with local variables and the parameters of the function or method of same name.

class Car{ constructor(brand){ this.name = brand; } brandName(){ return this.name; } }

A class Car is declared with member method brandName that returns the brand name.

var car1 = new Car("Toyota"); console.log('Brand:', car1.brandName()); var car2 = new Car("Audi"); console.log('Brand:', car2.brandName()); var car3 = new Car("BMW"); console.log('Brand:', car3.brandName()); Toyota' Audi BMW

Each object of the Car calls the method BrandName that returns the value stored in the variable name of the respective object.

throw

throw keyword force the JavaScript interpreter to trigger an error

throw "Custom-Error"; Uncaught Custom-Error

Custom error can be an object,

var rollNumber = -1; if(rollNumber <= 0) throw {errCode:604, message:'Invalid rollNumber: ' + rollNumber}; Uncaught {errCode: 604, message: 'Invalid rollNumber: -1'}

true & false

true & false keywords are used as boolean values in JavaScript

var title = 'Tutorial';

In the follwoing code snippet, since the variable title value is not equal to 'tutoreal'

var f = title === 'tutoreal'; console.log('f:', f); f: false

So the comparison operator === assigns false to f.

On the other hand, since the variable title value is not equal to 'Tutorial'

var t = title === 'Tutorial'; console.log('t:', t); t: true

since, comparison operator === assigns true to t.

try, catch & finally

try, catch and finally keywords are used in error-handling at run time.

typeof

JavaScript typeof keyword reveals the data type of variable or expression.

typeof "PiTribe"; string typeof 0; number

undefined

undefined is the default value of each variable until a value is assigned.

var v; console.log(typeof v); undefined

JavaScript undefined keyword lets the interpreter know that an identifier is not defined. Please refer the undefined chapter of this tutorial.

var

var s="string value";

JavaScript var keyword is used to declare a variable with following merits and demerits:

  • var may declare an identifier with multiple times
  • JavaScript hoists the variables who are declared with var keyword.

void

JavaScript void keyword.

while & do

while defines a while loop that checks the condition first and keeps iterating while the condition is true. do starts the do while loop body that termintates if the condition gets false in the while.

var i = 0; while(i < 10){ console.log(i++); } var i = 0; do{ console.log(i++); } while(i < 10); 0 1 2 3 4 5 6 7 8 9

Both the code snippets above, of while and do while, generate same output but the condition checked is different.
while loop checks the condition first whereas do while loop makes iteration at least once and checks the condition later.

yield