Device supports touch events

JavaScript, Browser · Oct 22, 2020

Checks if touch events are supported.

  • Check if 'ontouchstart' exists in the Window.
const supportsTouchEvents = () =>
  window && 'ontouchstart' in window;
supportsTouchEvents(); // true

Written by Angelos Chalaris

I'm Angelos Chalaris, a JavaScript software engineer, based in Athens, Greece. The best snippets from my coding adventures are published here to help others learn to code.

If you want to keep in touch, follow me on GitHub or Twitter.

More like this

  • Add multiple listeners

    Adds multiple event listeners with the same handler to an element.

    JavaScript, Browser · Oct 22, 2020

  • Handle user input change

    Runs the callback whenever the user input type changes (mouse or touch).

    JavaScript, Browser · Oct 21, 2020

  • Detect device type

    Detects whether the page is being viewed on a mobile device or a desktop.

    JavaScript, Browser · Oct 22, 2020