4

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?

Community
  • 1
  • 1
Stephan
  • 3,679
  • 3
  • 25
  • 42

4 Answers4

5

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.

robyaw
  • 2,274
  • 2
  • 22
  • 29
4

It means, if element.node has a value represents True in boolean expressions, node will be element.node, otherwise it will be element[element.length - 1].node

sinan
  • 6,809
  • 6
  • 38
  • 67
3

Simple answer: it means OR :)

Reference: http://www.w3schools.com/js/js_operators.asp

ChrisH
  • 1,283
  • 1
  • 9
  • 22
  • That part was quite obvious. The real question was: Was kind of weird "or" operation takes objects instead of boolean values and also returns an object instead of a boolean value? How is this "or" defined? Because it certainly isn't the standard definition according to logic. – Weirdo Aug 05 '21 at 05:23
1

It means OR

for example:

if this || that

means "if this or that"

So when the computer comes to the "||" part of your code, if the statement before it is true, it will stop reading that if statement and automatically execute the code underneath.

If the item before that "||" is false, then it will check the next one, and so on...

Musaab
  • 1,574
  • 3
  • 18
  • 37