Is there a null coalescence operator in javascript? Just like the C#? operator

Asked

Viewed 883 times

25

In C# there is an operator ?? which serves to make null coalescence, used following the syntax:

var valor = valorAnulável ?? valorPadrão;

Where:

  • valueApproachable is any value including null

  • default value is any value, usually not null

The operator returns the aValue value if it is not null, and if it is null, returns the default value. It is the same as saying:

var valor = valorAnulável != null ? valorAnulável : valorPadrão;

There is something like this in Javascript?

2 answers

25


Behaviour similar to that of the operator ?? of C# no javascript, using the or logical: ||.

There are however some differences, and for this we have to understand what the operator || of javascript means.

var resultado = valorA || valorB;

is exactly the same as:

var resultado = valorA ? valorA : valorB;

It turns out that in javascript, virtually all values can treated logical form, ie converted to true or false. As for the operation previous, when the value valorA is treated as true, the result of expression is its own value. When it is treated as false, the result of expression is the value valorB.

We must then understand what is treated as true and as false.

What is false:

  • empty string: ""
  • number 0 (zero)
  • false
  • null
  • Undefined
  • Nan

What is true:

  • all that is not false... including the following
  • empty strings: "0", "true", "false" (it is important to remember this)
  • all numbers different from 0: 1, -1, -1000, 1/10
  • true

Some examples

false || "texto qualquer"     // "texto qualquer"
"" || "texto qualquer"        // "texto qualquer"
0 || "texto qualquer"         // "texto qualquer"
null || "texto qualquer"      // "texto qualquer"
undefined || "texto qualquer" // "texto qualquer"

"algum texto" || "texto qualquer" // "algum texto"
1 || "texto qualquer"             // 1

Noted the difference to the ?? of C#... only if the first is null is that the second will be the result, otherwise the result is the first. Different from javascript where the second is the result, if the value of the first for falso, 0 or "".

1

Finally, the JS null coalescence operator proposal is advancing: TC39 Proposal nullish coalescing.

So (ES2020 ?) we should have this operator and the null navigation operator in the JS, which helps a lot in these scenarios:

someVariable.someProperty || 'some value'; 
// caso o valor seja '' ou 0, ira retornar o valor mais a direita, o que pode nao ser a intenção original

someVariable.someProperty ?? 'some value'; 
// neste caso apenas null e undefined serao checados

and we will have the operator of conditional null tb, already available in Angular 2+ through strings template, instead of writing:

if(someVariable && someVariable.someProperty) someVariable.someProperty.someFunction()

we can write:

someVariable.someProperty?.someFunction();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.