Nullish Coalescing Operator (??)

Cover by Evan Dennis on Unsplash

Summary

Intro
The operator ??
Use case
Considerations

Intro

You may have seen a Javascript piece of code like this before:

const value = actualValue || “defaultValue”;

The operator || …


This content originally appeared on DEV Community and was authored by Matheus dos Reis de Jesus

Cover by Evan Dennis on Unsplash

Summary

Intro

You may have seen a Javascript piece of code like this before:

const value = actualValue || "defaultValue";

The operator || used above is called "Logic OR", and works this way: If the value at the left of the operator is true, its value will be assigned to the variable value. If it is false, then the variable value will receive the value at the right, "defaultValue".

If you're used to Javascript, you must know that there are various problems with false and true values. Example: "" and 0 are considered false values. Then, if in an algorithm "" and 0 are considered valid values, the operator || wouldn't give the desired result. That's when the Nullish Coalescing operator comes.

The operator ??

Now that you understand the problem, comes the solution. The Nullish Coalescing operator is represented by the symbols ?? and is used like this:

const value = actualValue ?? "defaultValue";

In this case, if the value of actualValue is null or undefined, the value "defautlValue" will be assigned to the variable value. Otherwise, the value actualValue will be assigned to it.

Use case

Think about this scenario: You're creating a function that evaluates a expression using a coefficient. If the coefficient isn't provided, it will receive a default value, with 0 being valid. As I've said before, the operator || would not be the best for this case. Then, the operator ?? is much more suitable. Check the representation of this problem below:

function calculateResult(x,y,coefficient) { ;
    const c = coefficient ?? 0.5;
    return x + y * c;
}

const result = calculateResult(2,3);
console.log(result) // Output: 3.5 ( 2 + 3 * 0,5 )

The function calculateResult uses the ?? to check if the parameter coefficient is provided. If it is, then the value will be taken. Otherwise, the default coefficient will be 0.5.

Considerations

The case used was a simple example, but the operator ?? can be useful in a lot of situations and assure more reliability to your code.

Enjoyed this article? Follow me for more contents like this one!

Follow me:

Twitter | Instagram | Youtube.

See you on the next article!??


This content originally appeared on DEV Community and was authored by Matheus dos Reis de Jesus


Print Share Comment Cite Upload Translate Updates
APA

Matheus dos Reis de Jesus | Sciencx (2021-04-13T13:04:59+00:00) Nullish Coalescing Operator (??). Retrieved from https://www.scien.cx/2021/04/13/nullish-coalescing-operator/

MLA
" » Nullish Coalescing Operator (??)." Matheus dos Reis de Jesus | Sciencx - Tuesday April 13, 2021, https://www.scien.cx/2021/04/13/nullish-coalescing-operator/
HARVARD
Matheus dos Reis de Jesus | Sciencx Tuesday April 13, 2021 » Nullish Coalescing Operator (??)., viewed ,<https://www.scien.cx/2021/04/13/nullish-coalescing-operator/>
VANCOUVER
Matheus dos Reis de Jesus | Sciencx - » Nullish Coalescing Operator (??). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/13/nullish-coalescing-operator/
CHICAGO
" » Nullish Coalescing Operator (??)." Matheus dos Reis de Jesus | Sciencx - Accessed . https://www.scien.cx/2021/04/13/nullish-coalescing-operator/
IEEE
" » Nullish Coalescing Operator (??)." Matheus dos Reis de Jesus | Sciencx [Online]. Available: https://www.scien.cx/2021/04/13/nullish-coalescing-operator/. [Accessed: ]
rf:citation
» Nullish Coalescing Operator (??) | Matheus dos Reis de Jesus | Sciencx | https://www.scien.cx/2021/04/13/nullish-coalescing-operator/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.