What does “var FOO = FOO || {}” (assign a variable or an empty object to that variable) mean in Javascript?

前端 未结 7 1963
野性不改
野性不改 2020-11-22 02:07

Looking at an online source code I came across this at the top of several source files.

var FOO = FOO || {};
FOO.Bar = …;

But I have no ide

7条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 02:36

    The || operator takes two values:

    a || b
    

    If a is truthy, it will return a. Otherwise, it will return b.

    The falsy values are null, undefined, 0, "", NaN and false. The truthy values are everything else.

    So if a has not been set (is it undefined) it will return b.

提交回复
热议问题