What is the best way to detect a mobile device
You can use above script to do show/hide elements depending on the screen size.
Use Agent Detection (Not Recommended)
Detecting mobile device using user agent isn't the best way to check if a user is using a mobile device since user agent strings can be spoofed easily. However, this method is still an easy way to check what device is being used by the user.
To get the user agent string, you can use the navigator.userAgent property.
It is important to note that the value of the userAgent can be easily changed. For eg: when you use bots to scrape a website, you can pass a completely different user agent value to hide your identity. It will make it difficult to detect the actual device type .