Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does the "||" in a var statement mean? [duplicate]

Tags:

javascript

Possible Duplicate:
null coalescing operator for javascript?
What does “options = options || {}” mean in Javascript?

Can someone explain me this expression? I stumbled accros the javascript line of code and I wondered what it means.

var node = element.node || element[element.length - 1].node;

node get's used like this below:

if (node.nextSibling) {
            node.parentNode.insertBefore(this.node, node.nextSibling);
        } else {
            node.parentNode[appendChild](this.node);
        }

At first i though node should be a boolean or something but it's not. Am I correct if i think that the meaning is: node is element.node but if the node attribute is undefined node is the last element in the array of element?

like image 759
Stephan Avatar asked Dec 12 '22 09:12

Stephan


1 Answers

Your understanding is along the right lines; be aware that even if element.node is defined, but is a falsey value (0, false etc.) that element[element.length - 1].node will be assigned to node instead.

like image 114
robyaw Avatar answered Dec 26 '22 04:12

robyaw