'Click event listener working but not keydown?

Why does this work:

buttonGrid.addEventListener('click', e => {
  calculate(e);
});

let calculate = (e) => {
if (e.target.id === "back") {
displayArea.textContent = displayArea.textContent.substring(0, 
   displayArea.textContent.length - 1);
}
}

But not this...

buttonGrid.addEventListener('keydown', e => {
  calculate(e);
});

let calculate = (e) => {
if (e.keyCode === 8) {
displayArea.textContent = displayArea.textContent.substring(0, 
   displayArea.textContent.length - 1);
}
}

See lines 30-45 at: https://jsfiddle.net/s9ebLdvt/2/

If I log out the typeof e.keyCode I get number. Hence I use a number rather than a string.

I've also tried switching the === to == but get the same issue.

Weirdly if I place a console log in the if statement it fires it just doesn't compute the code?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source