
function getValue(optionalValue, defaultValue) {
return optionalValue ?? defaultValue;
}
function test() {
let value = getValue(null, 'default value');
console.log(value);
value = getValue('optional value', 'default value');
console.log(value);
}When developing JavaScript applications, we frequently encounter situations where a variable or function parameter might not contain a value. In such cases, providing a default value can help make our code more reliable and predictable.
JavaScript provides the nullish coalescing operator (??), introduced in ECMAScript 2020 (ES2020), to handle these situations in a simple and elegant way.
In this exercise, we will explore how to use this operator within a JavaScript function.
The JavaScript code
function getValue(optionalValue, defaultValue) {
return optionalValue ?? defaultValue;
}
function test() {
let value = getValue(null, 'default value');
console.log(value);
value = getValue('optional value', 'default value');
console.log(value);
}
Understanding the nullish coalescing operator
The nullish coalescing operator returns the value on its right side when the value on its left side is either null or undefined. Otherwise, it returns the value on its left side.
The general syntax is:
value ?? defaultValue
This allows developers to provide fallback values without writing explicit conditional statements.
Creating the getValue() function
Our function accepts two parameters:
function getValue(optionalValue, defaultValue) {
return optionalValue ?? defaultValue;
}
The optionalValue parameter represents the preferred value, while the defaultValue parameter represents the fallback value.
If optionalValue is null or undefined, the function returns defaultValue. In all other cases, the function returns optionalValue.
Testing the function
Test 1: Providing a null value
let value = getValue(null, 'default value');
console.log(value);
Since the first argument is null, the nullish coalescing operator returns the second argument.
Output:
default value
Test 2: Providing an existing value
value = getValue('optional value', 'default value');
console.log(value);
In this example, the first argument contains a valid string. Therefore, the function returns the first argument instead of the default value.
Output:
optional value
Nullish coalescing versus the logical OR operator
Before the introduction of the nullish coalescing operator, developers frequently used the logical OR operator (||) to provide default values.
However, there is an important difference between these two operators.
The logical OR operator considers all falsy values, whereas the nullish coalescing operator only considers null and undefined.
| Input value | value ?? 'default' | value \|\| 'default' |
|---|---|---|
null | 'default' | 'default' |
undefined | 'default' | 'default' |
false | false | 'default' |
0 | 0 | 'default' |
'' | '' | 'default' |
NaN | NaN | 'default' |
'Hello' | 'Hello' | 'Hello' |
This distinction is particularly important when working with numerical values, Boolean values or empty strings that represent legitimate application data.
Replacing conditional statements
Without the nullish coalescing operator, we could implement the same functionality using an if statement.
function getValue(optionalValue, defaultValue) {
if (optionalValue === null || optionalValue === undefined) {
return defaultValue;
} else {
return optionalValue;
}
}
Although both implementations produce the same result, the nullish coalescing operator makes the code considerably shorter while maintaining readability.
Practical application
Imagine an application that allows users to configure the volume of a music player.
const userVolume = 0;
const defaultVolume = 50;
const volume = userVolume ?? defaultVolume;
console.log(volume);
Output:
0
A volume of zero is a perfectly valid setting because the user might want to mute the music player.
If we had used the logical OR operator instead, the application would have incorrectly replaced the volume of zero with the default volume of 50.
Software testing considerations
From a software testing perspective, it is important to verify that the function behaves correctly for different input values.
The following test cases provide additional coverage beyond the two examples in our original exercise.
| Test case | Input | Default value | Expected result |
|---|---|---|---|
| TC01 | null | 'default' | 'default' |
| TC02 | undefined | 'default' | 'default' |
| TC03 | 'Hello' | 'default' | 'Hello' |
| TC04 | 0 | 100 | 0 |
| TC05 | false | true | false |
| TC06 | '' | 'default' | '' |
| TC07 | NaN | 100 | NaN |
These test cases help ensure that legitimate falsy values are not unintentionally replaced by default values.
Conclusion
The nullish coalescing operator is a powerful feature of modern JavaScript that simplifies the handling of missing values.
Unlike the logical OR operator, it preserves legitimate values such as zero, false and empty strings. This makes it particularly useful when working with configuration settings, function parameters, user preferences and data received from APIs.
By understanding and applying the nullish coalescing operator, developers can write cleaner, more concise and more predictable JavaScript code.