Code Synopsis logo

What are JavaScript Keywords?

A keyword instructs JavaScript to form certain expectations of upcoming code. Also known as reserved words, keywords have special meaning so you can’t use them as identifiers (e.g., the name of a function, variable, or label). Let’s look at a few JavaScript keywords and their implementations:

Declarations

  • var - depending on where it is placed, var declares a variable that is block-scoped or globally-scoped. You can optionally assign a value at the same time.
var i = 3;
  • let - introduced in ES6, let declares a variable that is always restricted to block-scope. Assigning a value is optional.
let i = 3;
  • const - also introduced in ES6, const declares a block-scoped variable that cannot be changed via reassignment. If the value is an object or array, then its properties can still be changed or deleted. Since the value cannot be reassigned, you must assign a value on declaration. It could be an empty array or object.
const i = 3;
  • class - declares a class – a function that creates an object using prototype-based inheritance. A class declaration is not hoisted. An existing class cannot be redeclared, but you can extend an existing class in a new class declaration.
class Car { // class body with optional constructor }
  • function - declares a function – instructions that accept input and return related output. Function declarations are hoisted. Note the use of the return keyword. This line delivers a final value to the function caller.
function addTwo(num) { let num2 = num + 2; return num2; }

Variable declaration keywords can be paired with a class or function keyword to create an expression statement. It serves to declare both the variable and the class or function and assign the class or function to the variable. The assigned item can be anonymous (unnamed).

var Car = class { // class body }

Unlike a function declaration, a function expression is not hoisted. If declared with a class expression, a class can be redeclared without a Syntax error, and it will always respond to “typeof” with “function.”

Tests

  • if... else - the if keyword presents a condition that, when met, allows a block of code to be executed. The else keyword is optional, and it precedes a block of code to be executed if the previously specified condition is false.
var i = 3; if (i === 3) { console.log('I see the number 3.'); } else { console.log('Must not be a 3.'); }
  • switch - the switch keyword allows you to store multiple conditions and responses in separate case blocks. You must include a break (see below) unless you want the default code to also run in that case.
const name = 'Joe'; switch (name) { case 'Joe': console.log('Hi Joe. Please call your mother.'); break; case 'Mary': case 'Sam': console.log('You have no messages.'); break; default: console.log('Sorry, I don’t recognize you.'); }
  • try... catch - the try keyword executes the next code block. If an exception is thrown, it is delivered to the ensuing catch block. You can optionally access the exception by passing a variable as an argument.
try { missing; } catch (e) { console.log(e.stack); } // ReferenceError: missing is not defined

It can be helpful to request the full stack of details. stack is a non-standard but universally accepted Mozilla property of the Error object .

  • throw - intentionally throw a user-defined exception (error). You can optionally create a new Error object using the throw new Error('message') syntax.
try { throw 'There is an error.'; } catch (e) { console.log(e); } // There is an error.

If you do not create an Error object, then the Error object properties are not available.

  • finally - execute code even if there’s an error caught. This is useful if you need something done even through the rest of the code will stop executing.
try { throw 'There is an error.'; } catch (e) { console.log(e); } finally { console.log('Finally.') } // There is an error. // Finally.

Loops and Iterations

Some keywords inform JavaScript to perform a loop or iterate through a group of values.

  • do... while - executes a block of code as a loop until the specified condition evaluates to false.
var i = 0; do { console.log(i); i++; } while (i < 4); // 0 1 2 3
  • for - executes a block of code in a loop according to conditions set by three optional expressions separated by semicolons.
for (var i = 0; i < 4; i++) { console.log(i); } // 0 1 2 3

The three expression statements of a for loop are often used to instantiate, test, and increment a counter (as shown above). The first statement executes before the first loop only. It’s ideal for instantiating a counter at 0. The second statement is a test of the input. When the condition is false, the next loop iteration will not run. The third expression executes after the iteration. If there are no expressions given you must include a break (defined below) in the code block, or the for loop will run indefinitely.

  • for... in - iterates over the enumerable properties of an object. With each iteration, the key is assigned to the variable. Note that the order of iterations depends on the browser implementation, so you can’t rely on a specific order.
var a = { a:1, b:2, c:3 }; for (const i in a) { console.log(a[i]); // i gets the key } // 1 // 2 // 3 // (but could also be: 3 1 2, etc)
  • for... of - iterates over the enumerable properties of an iterable object in the expected order. The property of each iteration is assigned to the variable.
var a = 'hello'; for (const i of a) { console.log(i); } // h // e // l // l // o
  • break - instructs JavaScript to stop the current loop and move to the next statement. If a label identifier is added after the break keyword, JavaScript will move to the labeled statement instead.
var i = 0; do { i++; if (i === 3) { break; } console.log(i); } while (i < 4); // 1 // 2
  • continue - used in conjunction with an if statement, continue interrupts the current iteration of a loop if the condition is true, and the loop continues without it.
var i = 0; do { i++; if (i === 3) { continue; } console.log(i); } while (i < 4); // 1 // 2 // 4

In the above example, the loop is interrupted when the input is four because that is when it fails the test.

  • return - ends function execution at the specified point and delivers a specified value to the function caller (or undefined if not specified). return can be used to end a loop, but, unlike break or continue , it must be used inside a function.
function count() { var i = 0; do { i++; if (i === 3) { return "I see 3."; } console.log(i); } while (i < 4); } console.log(count()); // 1 // 2 // 'I see 3.'

Remember to do something with the returned value. In the above example, we logged it to the console. You might want to assign it to a variable, for example.

Visit the references below to explore the complete list of JavaScript reserved words.

Related posts:

Helpful references:

Have feedback on this topic?