Skip to content Skip to sidebar Skip to footer

Two Pipe Symbols (or) In This Javascript Line

Possible Duplicate: What does “options = options || {}” mean in Javascript? I have seen this in JS: item = item || {}; I'm guessing it's some variation of a ternary operato

Solution 1:

(expr1 || expr2)

"Returns expr1 if it can be converted to true; otherwise, returns expr2."

source

So when expr1 is (or evaluates to) one of these 0,"",false,null,undefined,NaN, then expr2 is returned, otherwise expr1 is returned

Solution 2:

It's called redundancy, but in this case it's a good thing. Basically, if item is not defined (or otherwise falsy (false, 0, ""...), then we give it a default value.

Most common example is in events:

evt = evt || window.event;

Solution 3:

If item exists, set item to item, or set it to {}

Solution 4:

It equates to:

if(!item){item= {};}

Post a Comment for "Two Pipe Symbols (or) In This Javascript Line"