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?
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.
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.
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.
(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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With