How to check for the subtract key press with JavaScript?

Sometimes, we want to check for the subtract key press with JavaScript.

In this article, we’ll look at how to check for the subtract key press with JavaScript.

How to check for the subtract key press with JavaScript?

To check for the subtract key press with JavaScript, we can listen to the keypress event and use the event key property to check which key is pressed.

For instance, we write:

window.onkeypress = (e) => {
  if (e.key === '-') {
    console.log('subtract pressed')
  }
}

to check if the subtract key is pressed with e.key === '-' in the window.

If it’s pressed, then we log 'subtract pressed'.

Conclusion

To check for the subtract key press with JavaScript, we can listen to the keypress event and use the event key property to check which key is pressed.