The first way is to get the the actual device type or the OS name and figure it out from there. But this methods is not properly supported by browsers anymore.
const userAgent = navigator.userAgent;
let isMobile = Boolean(userAgent.match(
/Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile|WPDesktop/i
))
There is a second way (which I personally prefer) to check if the device our app is running on, is a touchscreen or not. If yes, then 9/10 cases it is a mobile device.
const isTouchDevice => () = {
return
(('ontouchstart' in window) ||
(navigator.maxTouchPoints > 0) ||
(navigator.msMaxTouchPoints > 0));
}
Note : we should not use any of these methods for styling or UI logic. A good use case for these client scripts is to allow or restrict heavy background processes (suggestion algorithms, analytics)