Detect a mobile device using JavaScript

Sometimes you may need to detect a mobile device for different operations. So in this article, we will show you how to detect a mobile device using JavaScript.

There are so many methods to find a mobile device but we will show you two methods.

Detect a mobile device using JavaScript

  1. Using userAgent
  2. Using regular expression

1. Using userAgent

You can do this by simply running through a list of devices and checking if the userAgent matches anything like so.

2. Using regular expression

Let’s use the following Regular Expression which returns true or false depends on the access device.

For those wishing to include tablets in this test, you can use the following function.

I hope you find this article helpful.
Thank you for reading. Happy Coding..!! 🙂

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

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