In one line of javascript:
var isMobile = ('ontouchstart' in document.documentElement && /mobi/i.test(navigator.userAgent)); If the user agent contains 'Mobi' (as per MDN) and ontouchstart is available then it is likely to be a mobile device.
EDIT: Updates the regex code in response to feedback in the comments. Using regex/mobi/i the i makes it case-insensitive, and mobi matches all mobile browsers. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent/Firefox