JavaScript detect mobile or tablet | HTML Example code

The Simplest way is using vanilla JavaScript navigator API to detect mobile or tablet in HTML webpage. You have to regex (regular expression) in the query.

You can also use a 3rd party library such as

if (WURFL.is_mobile === true && WURFL.form_factor === "Tablet") {
    // targetSmartPhoneDevices();

Example of JavaScript detect mobile or tablet

Using a console log to print message, IT will print message for tablet and mobile.

You can use it detect if the device is ONLY tablet or mobile.

<!DOCTYPE html>
    <script type="text/javascript">
        const userAgent = navigator.userAgent.toLowerCase();

        var isMobile = /iPhone|Android/i.test(navigator.userAgent);

        const isTablet = /(ipad|tablet|(android(?!.*mobile))|(windows(?!.*phone)(.*touch))|kindle|playbook|silk|(puffin(?!.*(IP|AP|WP))))/.test(userAgent);

        if(isMobile) {
        }else if(isTablet){




JavaScript detect mobile or tablet

All major browser provides a very useful API to know the user’s device and the browser details.


Do comment if you have better example or doubts or suggestions this topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.