DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,155 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for The Empty Statement in JavaScript

The Empty Statement in JavaScript

The empty statement in JavaScript is one of those fun and quirky things about JS that you should know. It might not be beneficial, but it exists, and it is entirely legal. An empty statement in JavaScript is ;. Yup, a semicolon.

An empty statement provides no statement even though JavaScript expects it. The statement has no effect and performs no action.

A typical example would be to create a for loop that has no body.

const arr = [1, 2, 3, 4, 5];

for (i = 0; i < arr.length; arr[i++] = 0) ;

console.log(arr); // [ 0, 0, 0, 0, 0 ]

It is a good idea to leave a comment if you ever plan on using an empty statement.

const arr = [1, 2, 3, 4, 5];

for (i = 0; i < arr.length; arr[i++] = 0) /* empty */ ;

console.log(arr); // [ 0, 0, 0, 0, 0 ]

Another example of using an empty statement is a chain of if-else.

const name = "Lauren";

if (name === "Parwinder")
    console.log(name);
else if (name === "Lauren")
    console.log(`Hello ${name}`); // Hello Lauren
else if (name === "Eliu"); // No action is taken if name passed is  "Eliu"
else if (name === "Robert")
    console.log(`Good to see you ${name}`);
else
    console.log("Goodbye");

Top comments (5)

Collapse
 
hi_artem profile image
Artem

Actually empty statements in js can have some legitimate use cases. For example, to prevent undesired behavior in IIFE you sometime want to prefix it with empty statement, like so:

;(()=>console.log('ooof'))()
Collapse
 
bhagatparwinder profile image
Parwinder πŸ‘¨πŸ»β€πŸ’» Author • Edited on

Ah thanks! Figured out when and what sort of undesirable behavior. If we are concatenating two JS files it could definitely cause issues. Here’s a write up with an example:

mariusschulz.com/articles/disassem...

Collapse
 
hi_artem profile image
Artem

Sure, and my bad, I should have specified it in the original post!

Collapse
 
jtenos profile image
Joe Enos

For those examples, rather than commenting, I'd just stick with an empty block for clarity:

for (...whatever...) { }

or

if (a) {
    doSomething();
} else if (b) {
} else if (c) {
    doSomethingElse();
}

Fewer characters and you won't have people swearing at you when they read the code.

Collapse
 
bhagatparwinder profile image
Parwinder πŸ‘¨πŸ»β€πŸ’» Author

Thanks for the feedback. Yup, that is why I specified if you ever plan on using an empty statement. If you don't and stick to empty blocks there is nothing to worry about.

This post blew up on DEV in 2020:

js visualized

πŸš€βš™οΈ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! πŸ₯³

Happy coding!