Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Sometimes you just need to do something the old way:

  let element = document.getElementById('some-element');
  while (element) {
    // do something
    element = element.parentElement;
  }
let doesn't get in the way with behavior such as hoisting, and the code will break if you redeclare a variable.


How does var/hoisting get in the way of that, or at least, that particular, construct?

FWIW, that would also be a nice "for" loop :-)

    var el
    for (
        el = document.getElement... ;
        el ;
        el = el.parentElement
    ) {
        // do something
    }




Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: