Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two pipe symbols (OR) in this Javascript line [duplicate]

Tags:

javascript

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 operator but what does is actually do?

like image 709
benhowdle89 Avatar asked Apr 27 '12 22:04

benhowdle89


People also ask

What does the pipe symbol mean in JavaScript?

The JavaScript Pipeline Operator ( |> ) is used to pipe the value of an expression into a function. This operator makes chained functions more readable. This function is called using ( |> ) operator and whatever value is used on the pipeline operator is passed as an argument to the function.

What does double pipe mean in Java?

The double pipe operator (||) is the logical OR operator . In most languages it works the following way: If the first value is false, it checks the second value. If it's true, it returns true and if it's false, it returns false.

What does single pipe mean in JavaScript?

A single pipe is a bit-wise OR. Performs the OR operation on each pair of bits. a OR b yields 1 if either a or b is 1. JavaScript truncates any non-integer numbers in bitwise operations, so its computed as 0|0 , which is 0.


1 Answers

(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

like image 60
ajax333221 Avatar answered Sep 23 '22 08:09

ajax333221