Skip to content

JavaScript keycode space bar | Example code

  • by

JavaScript keycode of the space bar is 32. These keycode values are only valid during keydown and keyup events. On Mac, keypress events give you a completely different set of codes.

Must Read: JavaScript keycode list

Example spacebar key code JavaScript

HTML example code executes JS code after pressing the space bar.

<!DOCTYPE html>
<html>
<body>

  <script>
    document.body.onkeyup = function(e){
      if(e.keyCode == 32){
        alert(e.code);
      }
    }

  </script>

</body>
</html>

Output:

JavaScript keycode space bar

Do comment if you have any doubts and suggestions on this JS Keycode tutorial.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *