javascript

JavaScript - Determine if user is on mobile device

You can use navigator.userAgent value to determine if the user is on a mobile device.

Abhith Rajan
Abhith RajanOctober 25, 2017 · 1 min read · Last Updated:

In the responsive design era, you may want to do some extra operations if the user is on mobile, like something I mentioned in my last post where my colleague wanted to remove some specific classes if the user is on mobile.

To do that, you can check the navigator.userAgent value. And with the help of regex you can do,

1var isMobile = /android.+mobile|ip(hone|[oa]d)/i.test(navigator.userAgent);

As you can see, we are matching against few of the major mobile user agents. But keep in mind that there are too many devices variants so user agents too, so if you need a more accurate result may be this answer on SO will be helpful.

Abhith Rajan

Written by Abhith Rajan
Abhith Rajan is an aspiring software engineer with more than nine years of experience and has a proven successful track record of delivering technology-based products and services.
Connect

This page is open source. Noticed a typo? Or something unclear?
Improve this page on GitHub

Related SnippetsView All

Related ArticlesView All

Related VideosView All

Easy NPM Package Updates with Yarn Upgrade Interactive

The Async Await Episode I Promised

Related StoriesView All