DEV Community

loading...
Cover image for noop in Javascript

noop in Javascript

pranesh
Web Dev.
・1 min read

Let's talk about "noop" in #javascript.

noop = no operation

As it's abbreviation says, it wont perform any operation. confusing? Let's see an example:

function noop(){}
const calculateSum = addFunction || noop;

As this example states, noop is like a default function and if "addFunction" dosen't exists, "calculateSum" takes noop. Meaning, it won't perform any operation. If "addFunction" is not defined and prevents "calculateSum()" from running undefined as function, it will run noop and returns undefined.

What if we use something like this:

const calculateSum = addFunction || () => undefined;

The above statement is absolutely fine, but by using named function such as noop, enhances readability for the user.

It is mostly used as backup callback function. I found it being used in a React dropdown library (downshift) also lodash has _.noop and so on.

TL;DR - It is a function, which performs no operation and once invoked it returns undefined . It helps improving the readability of the code and prevents application break.
Eg:

function noop(){}
// without noop:
const calculateSum = addFunction || () => undefined;
// with noop:
const calculateSum = addFunction || noop;

Discussion (0)