Backspace key code

Backspace, the Mac delete key, sends event.key "Backspace", event.code "Backspace" and keyCode 8.

Every value at a glance

Values for Backspace
Keyevent.codeevent.keykeyCodeevent.location
Backspace / DeleteBackspaceBackspace80 (Standard)

keyCode as Chrome and Edge send it on Windows. The Mac column (Chrome and Safari on a Mac) and the Firefox column show only numbers that differ; some keys also differ by system, as the text below says. event.which is always the same number as keyCode. event.key is for a US layout without Shift.

Press it here

Click here, then press Backspace

Using only a keyboard? Press Esc, then Tab, to leave the key box.

Code to react to Backspace

Recommended: match event.key
document.addEventListener('keydown', (event) => {
  if (event.key === 'Backspace') {
    // Your code here
  }
});
Match the key's place: event.code
document.addEventListener('keydown', (event) => {
  if (event.code === 'Backspace') {
    // Your code here
  }
});
Old code that still uses keyCode (deprecated)
document.addEventListener('keydown', (event) => {
  if (event.keyCode === 8) {
    // Your code here
  }
});

Which value to use

Check event.key === 'Backspace'. It’s the same on every layout, and event.code is "Backspace" too.

On a Mac the key is labelled delete, but it’s this key: it sends Backspace. A web page never sees a key named “delete” from it. The Mac’s forward delete (fn + delete) is the Delete key, keyCode 46.

keyCode 8 still arrives on computers, but it is deprecated.

What trips people up

Backspace does nothing on an Android phone

Many Android keyboards send keyCode 229 and key "Unidentified" for Backspace, like most keys; some send a real Backspace, depending on the keyboard and what you are typing. Listen for beforeinput instead, which fires before the text changes:

input.addEventListener('beforeinput', (event) => {
  if (event.inputType === 'deleteContentBackward') { /* Backspace */ }
});

This works with real keyboards too, so it can replace the keydown check in text fields. Ctrl + Backspace (Option + delete on a Mac) sends deleteWordBackward instead.

Code boxes don’t jump back on Backspace

One-time code inputs with one box per digit often move back to the previous box on Backspace. On Android the keydown says 229, so check event.key === "Backspace" in keydown and deleteContentBackward in beforeinput. In an empty box there is nothing to delete, and some Android keyboards then send no useful event at all. A single <input autocomplete="one-time-code"> avoids the problem.

Backspace goes back a page

Older browsers went to the previous page on Backspace when no text field had focus. Current Chrome, Edge and Firefox no longer do, so you don’t need to block Backspace to protect a form.

Deleting a selected item doesn’t work on a Mac

Many apps use Backspace or Delete to remove a selected item. Mac users press their delete key, which is Backspace, while Windows users press Delete. Accept both:

if (event.key === 'Backspace' || event.key === 'Delete') removeSelected();

Skip it when focus is in a text field, or the user loses an item while correcting a typo.

Backspace with modifiers

Cmd + delete on a Mac deletes to the start of the line. Shift + Backspace usually acts like plain Backspace. Check event.ctrlKey, event.altKey and event.metaKey before treating a press as a single-character delete.