Applying the Nullish Coalescing Operator on Runtime Function Invocations | Task

Ole Ersoy
Mar - 21  -  1 min

Scenario

We have a hello() function.

function hello(name: string = '') {
  return `hello ${name}`;
}

And we are using it in a code block where userName is a variable that may be null and if it is we wish the replace it with a default value Wilma.

Approach

We can use the Nullish Coalescing Operator like this:

hello(userName ?? 'Wilma')

Demo