You just need to check if the device is touch-sensitive.
const isMobileDevice = 'ontouchstart' in window || navigator.maxTouchPoints > 0;