JavaScript - Determine if user is on mobile device

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,

var 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 helpfull.


comments powered by Disqus

About the author

  • Abhith Rajan

    Abhith Rajan

    Abhith Rajan is a full-stack developer, consultant, Sysberries employee, and biker. Who passionate about C# and JavaScript. Works on his blog, his personal finance web app and its react native mobile app in his free time.

    Buy me a coffeeBuy me a coffee