Is there a "null coalescing" operator in JavaScript?

Daniel Schaffer

Is there a null coalescing operator in Javascript?

For example, in C#, I can do this:

String someString = null;
var whatIWant = someString ?? "Cookies!";

The best approximation I can figure out for Javascript is using the conditional operator:

var someString = null;
var whatIWant = someString ? someString : 'Cookies!';

Which is sorta icky IMHO. Can I do better?

Ates Goral

Update

JavaScript now supports the nullish coalescing operator (??). It returns its right-hand-side operand when its left-hand-side operand is null or undefined, and otherwise returns its left-hand-side operand.

Please check compatibility before using it.


The JavaScript equivalent of the C# null coalescing operator (??) is using a logical OR (||):

var whatIWant = someString || "Cookies!";

There are cases (clarified below) that the behaviour won't match that of C#, but this is the general, terse way of assigning default/alternative values in JavaScript.


Clarification

Regardless of the type of the first operand, if casting it to a Boolean results in false, the assignment will use the second operand. Beware of all the cases below:

alert(Boolean(null)); // false
alert(Boolean(undefined)); // false
alert(Boolean(0)); // false
alert(Boolean("")); // false
alert(Boolean("false")); // true -- gotcha! :)

This means:

var whatIWant = null || new ShinyObject(); // is a new shiny object
var whatIWant = undefined || "well defined"; // is "well defined"
var whatIWant = 0 || 42; // is 42
var whatIWant = "" || "a million bucks"; // is "a million bucks"
var whatIWant = "false" || "no way"; // is "false"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Null coalescing operator override

From Dev

Null coalescing operator with bool

From Dev

Null coalescing operator and casting with as

From Dev

Null coalescing operator override

From Java

PHP Null coalescing operator usage

From Dev

Null Coalescing Operator in F#?

From Dev

null coalescing operator assignment to self

From Dev

null-coalescing operator in the getter

From Java

PHP ternary operator vs null coalescing operator

From Dev

Usage of ?? operator (null-coalescing operator)

From Dev

Usage of ?? operator (null-coalescing operator)

From Dev

null-coalescing operator on a property get

From Dev

Implicit conversion with null-coalescing operator

From Java

What is null coalescing assignment ??= operator in PHP 7.4

From Dev

Is there an equal for the c# null coalescing operator in java?

From Dev

null-coalescing operator with wildcard in a lambda expression

From Dev

Is there a way to implement and make use of a "NOT null coalescing" operator?

From Dev

Is there an equivalent of isset or a null coalescing operator in SASS?

From Dev

sql null in c# and null coalescing operator

From Dev

Is there a way to implement and make use of a "NOT null coalescing" operator?

From Dev

Is there an equivalent of isset or a null coalescing operator in SASS?

From Dev

How to define a null-coalescing operator for Twig?

From Dev

Performance of expression behind null coalescing operator

From Java

Curious null-coalescing operator custom implicit conversion behaviour

From Dev

How does the Null-Coalescing Operator (??) work in Spider?

From Dev

Shorthand for null-coalescing operator and assignment for re-generatable property?

From Dev

Null-coalescing operator returning null for properties of dynamic objects

From Java

Null coalescing operator IList, Array, Enumerable.Empty in foreach

From Dev

Why doesn't the null coalescing operator (??) work in this situation?

Related Related

  1. 1

    Null coalescing operator override

  2. 2

    Null coalescing operator with bool

  3. 3

    Null coalescing operator and casting with as

  4. 4

    Null coalescing operator override

  5. 5

    PHP Null coalescing operator usage

  6. 6

    Null Coalescing Operator in F#?

  7. 7

    null coalescing operator assignment to self

  8. 8

    null-coalescing operator in the getter

  9. 9

    PHP ternary operator vs null coalescing operator

  10. 10

    Usage of ?? operator (null-coalescing operator)

  11. 11

    Usage of ?? operator (null-coalescing operator)

  12. 12

    null-coalescing operator on a property get

  13. 13

    Implicit conversion with null-coalescing operator

  14. 14

    What is null coalescing assignment ??= operator in PHP 7.4

  15. 15

    Is there an equal for the c# null coalescing operator in java?

  16. 16

    null-coalescing operator with wildcard in a lambda expression

  17. 17

    Is there a way to implement and make use of a "NOT null coalescing" operator?

  18. 18

    Is there an equivalent of isset or a null coalescing operator in SASS?

  19. 19

    sql null in c# and null coalescing operator

  20. 20

    Is there a way to implement and make use of a "NOT null coalescing" operator?

  21. 21

    Is there an equivalent of isset or a null coalescing operator in SASS?

  22. 22

    How to define a null-coalescing operator for Twig?

  23. 23

    Performance of expression behind null coalescing operator

  24. 24

    Curious null-coalescing operator custom implicit conversion behaviour

  25. 25

    How does the Null-Coalescing Operator (??) work in Spider?

  26. 26

    Shorthand for null-coalescing operator and assignment for re-generatable property?

  27. 27

    Null-coalescing operator returning null for properties of dynamic objects

  28. 28

    Null coalescing operator IList, Array, Enumerable.Empty in foreach

  29. 29

    Why doesn't the null coalescing operator (??) work in this situation?

HotTag

Archive