Pransh Tiwari
Pransh Tiwari

Reputation: 4202

Why 'null' is passed as argument in functions in javascript?

I was going through some code and couldn't understand why null is being passed as a program argument. Can someone please explain this?

callback(null, data);

The handler code:

function callbackHandler(error, ...rest) {
    if(error) {
        console.log(error);
    }
    else {
        console.log("No error"+rest);
    }
}

Upvotes: 1

Views: 184

Answers (1)

manojlds
manojlds

Reputation: 301387

It's a pattern - the first argument if not null indicates some error occurred in the operation.

err = { "message": "Oops!"};
callback(err, null);

If it is null, the operation in question has completed successfully and is giving back the data via the callback.

Upvotes: 4

Related Questions