Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling the distinction between undefined- and null-parameters in JavaScript

Tags:

javascript

I know very well that null and undefined are distinct in JavaScript. However, I can't seem to decide whether or not use that fact when my own functions are passed one of those as its argument.

Or, expressed in a different way, should myFoo(undefined) return the same thing as myFoo(null)?

Or, in yet another case, since myBar(1, 2, 3) is (almost) the same thing as myBar(1, 2, 3, undefined, undefined), should myBar(1, 2, 3, null, null) return the same thing as myBar(1, 2, 3)?

I feel that there's potential for confusion in both cases and that a library should probably follow a convention when handling null/undefined.

I'm not really asking for personal opinions (so please express those as comments rather than answers). I'm asking if anyone knows if there is a best practice that one should stick to when it comes to handling this distinction. References to external sources are very welcome!

like image 830
Jakob Avatar asked Jun 02 '10 16:06

Jakob


People also ask

What is the difference between null and undefined in JavaScript?

Definition: Null: It is the intentional absence of the value. It is one of the primitive values of JavaScript. Undefined: It means the value does not exist in the compiler.

What is difference between undefined and null in JavaScript illustrate with an example?

In JavaScript, undefined is a type, whereas null an object. It means a variable declared, but no value has been assigned a value. Whereas, null in JavaScript is an assignment value. You can assign it to a variable.

What is the difference between undefined value and null value?

The null value is a primitive value which represents the null, empty, or non-existent reference. The undefined value is a primitive value, which is used when a variable has not been assigned a value.

How is undefined different from null?

Difference Between undefined and nullundefined is a variable that refers to something that doesn't exist, and the variable isn't defined to be anything. null is a variable that is defined but is missing a value.


1 Answers

I'd say that while, most of the time, there is little value in distinguishing between the two, the cases where there is value tend to be quite interesting.

Take, for example, a function which can be given a callback. undefined might indicate that some default callback should be used (as if the parameter weren't specified), but null could indicate that no callback should be made at all:

function asyncWorker(data, callback, timeout) {
    if (typeof callback === "undefined") {
        callback = function() { $("#log").append("<p>Done!</p>"); };
    }

    // ...

    if (callback) callback();
}

asyncWorker([1, 2, 3]); // default callback, no timeout
asyncWorker([4, 5, 6], null); // no callback, no timeout
asyncWorker([7, 8, 9], undefined, 10000); // default callback, 10s timeout

Of course, false or 0 could be used instead of null here, but that might not be the case in a more complex example. And whether your code benefits from the additional parameter complexity is entirely up to you. :-)

like image 93
Ben Blank Avatar answered Oct 02 '22 12:10

Ben Blank