Javascript for nav highlight not working in IE

I have a great little script for highlighting links on a one page site that I found here on Stack Overflow. It works perfectly in Chrome and Edge but not in IE. Here is a snippet. Any suggestions?

<header>
<nav>
<a href="#one">ONE</a>
<a href="#two">TWO</a>
<a href="#three">THREE</a>
<a href="#four">FOUR</a>
<a href="#five">FIVE</a>
</nav>
</header>
<main>
<section id="one">
<section id="two">
<section id="three">
<section id="four">
<section id="five">
</main>

<script>
const links = document.querySelectorAll('nav a');
const sections = document.querySelectorAll('section');

function changeLinkState() {
  let index = sections.length;

  while(--index && window.scrollY + 1 < sections[index].offsetTop) {}

  links.forEach((link) => link.classList.remove('active'));
  links[index].classList.add('active');
}

changeLinkState();
window.addEventListener('scroll', changeLinkState);
</script>

104 thoughts on “Javascript for nav highlight not working in IE”