JavaScript ||
operator can be used as equivalent of C# ??
operator.
How ?? operator works
C# ??
operator evaluate first operand or expression at left-side, if result is null
then evaluates second operand or expression at right-side in same manner.
If the left-hand operand not null
, then right hand operand not evaluated by C# compiler.
C# Example of ?? operator
String x = null;
String y = x ?? "Axe"; // y = Axe, because x is null so set the hard coded string Axe
String z = y ?? "Zee"; // z = Axe, because y was not null so set the value of y in z
JavaScript ||
may work as null coalescing operator as ??
do in C#. ||
returns first value (at left side of operand) of its left side operand if not null
. If null
, then evaluate the right side operand, i.e. second operand value.
JavaScript example of ?? operator equivalent ||
var x = null;
var y = x || "Axe"; // y = Axe, because x is null so set the hard coded string Axe
var z = y || "Zee"; // z = Axe, because y was not null so set the value of y in z
What is ?? operator
C# ??
is Null Coalescing operator for C Sharp language of .Net framework and .Net Core.
What is Null coalescing operator
Null Coalescing operator takes to operands, returns not null operand (object value) giving first operand priority i.e. if first operand at left side of Null Coalescing operator is not null
then returns it, otherwise returns second at left.
If both operand are null around Null coalescing operator
returns null
if both operand are null
.
Posted Status in Programming