JavaScript Nullish Coalescing OperatorThe double question mark (??) denotes the nullish coalescing operator introduced in ES6. When its left-hand side operand is undefined or null, the nullish coalescing (??) operator returns its right-hand side operand. In all other cases, the logical operator returns its left-hand side operand. The nullish coalescing operator is the logical operator to get the right side operand for display output by default. SyntaxThe following syntax shows the basic operation of the nullish coalescing operator. There are two values that the nullish coalescing operator will accept: If the first value (second_value) is null or undefined, the nullish coalescing operator returns the second value (second_value). The old method for the nullish coalescing operatorThe following block corresponds to the nullish coalescing operator: Any undefined or null value is considered to have a nullish value. How to operate the nullish coalescing operatorThe nullish coalescing operator (??) is used in the example below to return the string "Javatpoint" because the first value is null: The following syntax shows the output const mark = undefined ?? 80; console.log(mark); Examples The following examples show the nullish coalescing operator in JavaScript. The multiple values display using the logical and comparison operator. Example 1: The following example shows the basic use of the nullish coalescing operator in JavaScript. We can see the string and numerical data to compare with the null data. Output The following image shows two values as an output of the operator. Example 2: The following example shows the use of the nullish coalescing operator with a variable in JavaScript. Output The following image shows four values as an output of the operator. Example 3: The following example shows the use of the nullish coalescing operator with a function in JavaScript. We can see all the function's data in the console tab for different data of the functions attribute. Output The following image shows three values as an output of the operator. Example 4: The following example shows the use of the nullish coalescing operator with a constant variable in JavaScript. We can use a constant function as a function with the value. Output The following image shows three values as an output of the operator. Why use the nullish coalescing operator
Short-circuiting occurs in the nullish coalescing operator.
Examples The following examples show the nullish coalescing operator for short circuits in javascript. The multiple values display using the logical operator in the console, alert box, and div tag. Example 1: The following example shows the use of the nullish coalescing operator with the variable in JavaScript. Output The following image shows various values as an output of the operator. Output1 Example 2: The following example shows the Short-circuiting occurs in the nullish coalescing operator in JavaScript. We can see the javascript functionality with the null, not null, operand. Output The following images show the web page and console value using the operator. Output1 The image shows the first operand is not null. Output2 The image shows the first operand is null in the console. Using the AND or OR operator to chain
Example The following example shows that Short-circuiting occurs in the nullish coalescing operator in JavaScript. We can see the javascript functionality with the null, not null, operand. Output The following images show the value on the web page of the operator. SummaryWhen given two values, the nullish coalescing operator (??) gives the second item if the first value is null or undefined. The logical AND or OR operator cannot be combined directly with the javascriptnullish coalescing operator because it is short-circuited. Next TopicJavascript filereader |