Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null-coalescing in Javascript?

Tags:

javascript

In C# you can do the following:

string y = null; string x = y ?? "sup stallion"; //x = "sup stallion" since y is null. 

Where The ?? operator is the null-coalescing operator.

And in Javascript I've seen something similar:

var headers; var myHeader = headers || {'Content-type':'text/plain'}; //myHeaders = {'Content... 

I've also seen: (The 2nd code snippet on the page)

var headers; var myHeader = headers | {'Content-type':'text/plain'}; 

Is there a difference between the two? What is this pattern called...default parameter?

like image 778
Alan Avatar asked Mar 26 '12 01:03

Alan


People also ask

What is null coalescing operator in JavaScript?

The nullish coalescing operator ( ?? ) is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined , and otherwise returns its left-hand side operand.

When was null coalesce added to JavaScript?

JavaScript made sure this can be handled with its nullish operator also known as the Null Coalescing Operator, which was added to the language with ECMAScript 2020. With it, you can either return a value or assign it to some other value, depending on a boolean expression.

Why we use null coalescing operator?

operator is known as Null-coalescing operator. It will return the value of its left-hand operand if it is not null. If it is null, then it will evaluate the right-hand operand and returns its result. Or if the left-hand operand evaluates to non-null, then it does not evaluate its right-hand operand.

What are Nullish values in JS?

In JavaScript, a nullish value is the value which is either null or undefined . Nullish values are always falsy.


1 Answers

|| is a logical or. It returns the first truthy operand* (the last value evaluated). So

var myHeader = headers || {'Content-type':'text/plain'}; 

Returns "headers" if it's truthy (and if it's null or undefined, that value is coreced into "false"). If it's falsey, it returns the second operand. I don't believe this has a very specific name in javascript, just something general like "default argument value".

| is a bitwise or. It is a mathematical operation and does something completely different. That operator doesn't even make sense here (and it will generally just produce 0 as a result). Wherever you saw that, it was surely a typo, and they meant to use logical or.

So go with that first method (a = b || c).

* "Logical or" is also known as "logical disjunction" or "inclusive disjunction". Javascript, like all programming languages, evaluates logical statements using short-circuit evaluation. With logical-or expressions, it evaluates each operand for truthiness and stops on the first true one (and returns that value). If there are no truthy operands, it still had to go through all of them, so it returns the last operand, still the last one it evaluated. Logical and (&&) is similarly short-circuited by stopping on the first falsey operand.

like image 164
Ben Lee Avatar answered Nov 07 '22 13:11

Ben Lee