0

Per the following JavaScript, what happens if a property is assigned to a fallback and a default has not been specified for the fallback?

JavaScript statement in question:
options.content = options.content || '<div class="blue"></div>';

The function the statement belongs to:

function NimbusCloud(options) {
    options.content = options.content || '<div class="blue"></div>';

    this._statusElement = null;
    this._statusTemplate = new EJS({
        cache: true,
        name: 'cloud.nimbus',
        url: '/static/clouds/templates/nimbus_cloud.ejs'
    });

    Cloud.call(this, options);
}
phil o.O
  • 376
  • 3
  • 17

2 Answers2

2

This effectively provides a fallback default if options.content hasn't been set to anything else.

Rapta
  • 57
  • 4
1

Initializing a variable to itself or to a value is a standard pre ES6 way of using defaults.

options.content = options.content || '<div class="blue"></div>';

The statement is translated to if options.content is not falsy (undefined, null, 0, false, empty string, etc...) , use options.content, if it is falsy use the default '<div class="blue"></div>'.

Ori Drori
  • 145,770
  • 24
  • 170
  • 162