What does "options = options || {}" mean in Javascript? [duplicate]

Martin.ptrn提出了一个问题:What does “options = options || {}” mean in Javascript? [duplicate],或许与您遇到的问题类似。


This is useful to setting default values to function arguments, e.g.:

function test (options) {
  options = options || {};

If you call test without arguments, options will be initialized with an empty object.

The Logical OR || operator will return its second operand if the first one is falsy.

Falsy values are: 0, null, undefined, the empty string (""), NaN, and of course false.