Logical OR Assignment in JavaScript

Level 3113 keystrokes

The ||= operator assigns the right-hand value only if the left-hand value is falsy.

JavaScript code

// Create a config object with falsy values
1const config = { debug: false, timeout: 0 };
// ||= assigns true because debug is false (falsy)
2config.debug ||= true;
// ||= assigns 5000 because timeout is 0 (falsy)
3config.timeout ||= 5000;
// Output shows both values were reassigned: { debug: true, timeout: 5000 }
4console.log(config);

Output

{ debug: true, timeout: 5000 }
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.

  • ;× 4
  • |× 4
  • .× 3
  • =× 3
  • :× 2
  • ,× 1
  • (× 1
  • )× 1
  • {× 1
  • }× 1

More JavaScript examples at level 3

← JavaScript typing practice