0

I'm curious to know that what does a ?? mean in JS?

form.inputs.forEach(input => {
      stepProperties[input.field] = {
        info: input.fieldInfo,
        description: input.fieldLabel,
        default: input.fieldDefault ?? '',
        ...getInputType(input.fieldType, fieldTypes),
      };
      itemToPush.properties = stepProperties;
    });

in the above code snippet the value of default key is input.fieldDefault ?? ''

would like to understand this line.

I did google but did not get any proper answer.

spratap124
  • 151
  • 2
  • 10
  • if input.fieldDefault = 'false' then what value default key will hold ? – spratap124 Mar 08 '22 at 06:46
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator – mamady Mar 08 '22 at 06:47
  • 1
    I would suggest googling "javascript double question mark operator". It then shows results for Nullish coalescing operator (??) – Miro Mar 08 '22 at 06:49

4 Answers4

1

It's the nullish coalescing operator, if the left side is null or undefined it will return the right side

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator

Alan Dávalos
  • 2,568
  • 11
  • 19
0

?? is the "nullish coalescing operator" a special case of the "||". It returns the right side operand if the left side operand is falsy.

Source

amunso
  • 11
  • 1
  • 2
0

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.

let input = {};
let variable = input.fieldDefault;

console.log(`variable is undefined : ${variable == undefined}`);

variable = input.fieldDefault ?? '';

console.log(`variable is undefined : ${variable == undefined}`);
console.log(`variable is empty string : ${variable == ''}`);
Ian
  • 1,198
  • 1
  • 5
  • 15
0

What is ?? 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.

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator

Example:

var a = null ?? "cat"; // sets "a" to "cat"
var b = undefined ?? "dog"; // sets "b" to "dog"
var c = "some string" ?? "mouse"; // sets "c" to "some string"

How to google these things?

I would suggest googling "javascript double question mark operator" or something similar. It then shows results for Nullish coalescing operator (??). Using symbols such as "?" in google search query often doesn't give expected results.

Miro
  • 1,778
  • 6
  • 24
  • 42