null

The null primitive type is used to express the intentional absence of a value. It is a type with only one value: the only null value is null.

The semantics of null are crucially different from undefined. The undefined value is used to indicate something that is not declared or defined, while null is an explicitly declared absent value. We usually use the null value to indicate that a value is either explicitly not yet set or, for whatever reason, unavailable.

For example, let's consider an API where we specify various properties related to a restaurant review:

setRestaurantFeatures({
hasWifi: false,
hasDisabledAccess: true,
hasParking: null
});

The null value, in this context, means that we do not know the value of hasParking yet. When we have the necessary information, we can specify hasParking as either true or false (Boolean), but to express our ignorance of its true value, we're setting it to null. We could also completely leave the value out, meaning that it would effectively be undefined. The key difference is that using null is always proactively done, while undefined is the result of wh something isn't done.

A null value, as we mentioned previously, is always falsy, meaning that it will always evaluate to false in a Boolean context. So, if we attempt to use null in a conditional statement, then it would not succeed:

function setRestaurantFeatures(features) {
if (features.hasParking) {
// This will not run as hasParking is null
}
}

It is important to check for the exact values we want so that we can avoid bugs and communicate effectively to the people reading our code. In this case,  we may wish to explicitly check for undefined and null as we want to execute distinct code for that case versus the case of false. We could accomplish this like so:

if (features.hasParking !== null && features.hasParking !== undefined) {
// hasParking is available...
} else {
// hasParking is not set (undefined) or unavailable (null)
}

We can also use the abstract equality operator (==) to compare to null, which will helpfully evaluate to true if the operand is either null or undefined:

if (features.hasParking != null) {
// hasParking is available...
} else {
// hasParking is not set (undefined) or unavailable (null)
}

This is, in fact, doing the same as the more explicit comparison, but is far more succinct. Unfortunately, it's not very clear that its intention is to check for both null and undefined. We should usually prefer being explicit as this allows us to communicate our intent to other programmers in a more efficient way. 

A final trap to avoid with null is the typeof operator. Due to some legacies of the JavaScript language, typeof null will, rather confusingly, return "object" and is therefore entirely unreliable. 

More information about typeof and detection of the null type can be found in Chapter 7, Dynamic Typing, in the Detection section.

So, there you have it. Null is a simple enough value and, insofar as clean code is concerned, you won't go wrong if you remember two key points: that it should only be used to express the intentional absence of a value and that it should, ideally, be checked explicitly (prefer value === null). 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.200.46