Reserved keywords in ES6 with example usage

Jakub Włodarczyk
5 min readMar 15, 2018

--

There’s quite an impressive number of reserved keywords for the ES6 that some people are unaware of. Probably it would be hard for anyone to name them all so let’s create a kind of a cheatsheet with some practical use for them.

First, let’s see what happens when reserved keywords are used inappropriately:

Invalid usage of reserved keywords

Function, switch and for are all reserved keywords and as we can see in the examples, invalid usage of them will cause an error to be thrown.

Without further ado, let’s take a look at the list of the reserved keywords:

break — terminates a loop or a switch construct

for (let i = 0; i < 10; i++) {
if (i === 5) {
break;
}
console.log(i); // 0, 1, 2, 3, 4
}

case — it’s a clause that compares an expression from the switch statement

const expr = 5;
switch(expr) {
case 4:
console.log(4);
break;
case 5:
console.log(expr); // will print 5
break;
default:
console.log('default');
}

catch — catches an exception in a try statement

try {
throw new Error('some error');
} catch (e) {
console.log(e.message); // some error
}

class — declares a class (classes are based on prototypal inheritance)

class Some {}
typeof Some // function

const — keyword for declaring a constant

const constant = 5;
constant = 6; // Uncaught TypeError: Assignment to constant variable

continue — skips a current step in a loop

for (let i = 0; i < 3; i++) {
if (i === 1) {
continue;
}
console.log(i); // 0, 2
}

debugger — breaks execution of a script and enables the debug mode

function debugMe() {
debugger;
}

Calling the debugMe will result in such behavior in Chrome:

Debugger in action

default — can be used as a keyword for a default export or within a switch statement when no cases are matched against an expression

const expression = 1;
switch(expression) {
case 2:
console.log(2);
break;
default:
console.log('default behavior'); // this will be called
}

delete — deletes a property from an object

const obj = {
deleteMe: true
}
console.log(obj); // {deleteMe: true}
delete obj.deleteMe;
console.log(obj); // {}

do — used in a do…while loop. It makes a loop execute at least once.

do {
console.log('executed'); // will be printed once
} while (false);

else — used with an if statement. It will execute when a condition is falsy

if (false) {
console.log('won\'t be called');
} else {
console.log('will be called'); // called
}

export — export some construct to be used by calling an import statement

const func = () => 5;
export default func;

extends — creates a child of another class

class ChildClass extends ParentClass {}

finally — will be called near a try…catch construct no matter if exception was caught or thrown

try {
throw new Error('some error');
} catch (e) {
console.log(e.message); // some error
} finally {
console.log('finally is called'); // this will be called as well
}

for — creates a loop

for (let i = 0; i < 1; i++) {}

function — defines a function

function someFunc() {}

if — executes a truthy condition

if (true) {
// do sth
}

import — imports an exported construct

const func = () => 5;
export default func;
import func from (...)

in — returns true if a property exists in an object

const checkIn = {
prop1: 'a property'
}
console.log('prop1' in checkIn); // true
console.log('prop2' in checkIn); // false

instanceof — checks the instance of a construct

function some() {}
console.log(some instanceof Function); // true
class SomeClass {}
const child = new SomeClass;
console.log(child instanceof SomeClass); // true

new — creates an instance of some construct

class SomeClass {}
const child = new SomeClass;
console.log(child); // SomeClass {}

return — returns some value or terminates function execution

function returnVal() {
return 'string';
}
console.log(returnVal()); // string

super — calls parent method within a class or an object

class Main {
someFunc() {
console.log('in someFunc()');
}
}
class SomeChild extends Main {
extendedFunctionality() {
// some new functionality
super.someFunc();
}
}
const el = new SomeChild;
el.extendedFunctionality(); // in someFunc()

switch — evaluates some expression and executes some code based on case clauses

switch(expr) {
// handle cases and default
}

this — refers to the execution context

const objContext = {
someFunc() {
console.log(this);
}
}
console.log(objContext.someFunc()); // {someFunc: ƒ} --> execution context is someFunc function

throw — terminates execution of a function and throws an exception or error

throw new Error('err'); // Uncaught Error: err

try — used to try some code, should be mainly used with external code that we don’t have control over

try {
// try some code
} catch (err) {
// handle error
|

typeof — returns type of an operand

const str = 'string'; // typeof str --> "string"
const num = 5; // typeof num --> "number"
const bool = false; // typeof bool --> "boolean"

var — used to declare a variable

var myVar = 'variable';

void — evaluates some expression but no value is returned (undefined is returned instead)

console.log(void 5); // undefined
console.log(void 'string'); // undefined
console.log(void true); // undefined

while — creates a loop that will work as long as a condition is truthy

let j = 1;
while (j <= 4) {
j++;
}
console.log(j); // 5

with — provides a default object for some statement

// assume Math object for the random function
with(Math) {
console.log(random()); // 0.5289267443679495
}

yield — pauses/resumes a generator function

function* yieldExample() {
yield 2;
console.log('text'); // will not be called, generator is suspended
}

That would be all, can you count all of the reserved keywords? :) cheers!

--

--