Categories
JavaScript Answers

How to detect Android phone via JavaScript?

Spread the love

Sometimes, we want to detect Android phone via JavaScript.

In this article, we’ll look at how to detect Android phone via JavaScript.

How to detect Android phone via JavaScript?

To detect Android phone via JavaScript, we can check if the user agent has 'android' in it.

For instance, we write

const ua = navigator.userAgent.toLowerCase();
const isAndroid = ua.indexOf("android") > -1;
if (isAndroid) {
  //...
}

to get the user agent with navigator.userAgent.

Then we convert it to lower case with toLowerCase.

Next we check if 'android' is in the user agent string with indexof.

Conclusion

To detect Android phone via JavaScript, we can check if the user agent has 'android' in it.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *