Javascript: Index of node within parent

Published:

Apparently there isn't any node.index property built-in to get the index of a node in relation to its sibling nodes. So, here's how to find it, in case I (or someone else) need it again:

function indexOf(node) {
  let i = 0
  while ((node = node.previousElementSibling)) i++
  return i
}