Nullish Coalescing in JavaScript

Level 2108 keystrokes

Uses ?? operator to provide a default value when the left side is null or undefined.

JavaScript code

// Declare a variable with null (no value)
1let value1 = null;
// Declare a variable with a string value
2let value2 = "Hello";
// ?? returns the right side if left is null or undefined
// value1 is null, so "default" is returned
3console.log(value1 ?? "default");
// value2 has a value, so "Hello" is returned
4console.log(value2 ?? "default");

Output

default
Hello
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this JavaScript code and how often they appear.

  • "× 6
  • ;× 4
  • ?× 4
  • .× 2
  • (× 2
  • )× 2
  • =× 2

More JavaScript examples at level 2

← JavaScript typing practice