Select Page

TF is noop and what’s the use of it (JS)?

August 5, 2021

Have you encountered a function or a variable that is called noop in any source code you are reading? Have you asked yourself what the hell is that for?

Well, noop just means NO OPERATION. Meaning do not do something, return undefined or return an empty function. It is literally simple as that.

So what is the need for this noop at all?

Backup callback / Legacy code

Supposed you have a function like that accepts a function like so:

function displayInfo(callbackFn) {
  // execute callbackFn and and perform other complex things

and supposed your code will run in different environments like desktop, web, and mobile.

Now you need to provide displayInfo() a valid callback function that might only be available for a particular environment. Typically developers will create an empty function that will do nothing, thus noop. Like so:

 const callbackFn = environmentFn ? environmentFn : () => {} 

But writing an empty function every time you need it is not good in the eyes right? As such, developers separate it on a separate function like so:

const noop = () => {} 

That’s it! If ever you think of another usage of the noop function feel free to comment it below. 


Submit a Comment

Your email address will not be published. Required fields are marked *