How to Detect Mobile Devices using JavaScript18 Apr 2025 | 6 min read Many operations works on desktop, tablet, and mobile devices. We can detect the operation working on the mobile device. We can detect mobile devices using various methods, including the "window.navigator.userAgent" and "window.orientation" methods. Method 1: Making use of the guide.userAgent PropertyWe will use the window navigator object, which has all the information about a browser, to identify a mobile device using JavaScript. The userAgent field, which is used to return the user-agent header supplied to the server by the browser, will be the Property we utilize to identify a mobile device. The browser name, version, and platform are all the details about a mobile device. You will obtain them by using this function, and they are the values we get from this Property. Syntax: The JavaScript syntax for detecting a mobile device is as follows: Step to the Procedures The following steps outline how to use JavaScript to identify a mobile device:
Example The following example shows whether the device is mobile or not. The device shows the Boolean values for the detection of mobile devices. Output The output shows the Boolean values for the detection of mobile devices. ![]() Explanation We must execute your code using the Chrome developer tool and select the mobile phones option in order to get the output from the above code to be "true." You may use the code above to detect the presence of mobile phones by entering the developer tool's mobile zone and selecting any phone from the list of available devices to use as an emulator. When the code is executed, a window labeled "Check" will open. Click this button to see if the mobile phone can be detected. The outcome will be true if the code is executed on a mobile device and false otherwise. Method 2: use the window.orientation methodThe user-agent strings in this method can be easily spoofed. Therefore, it's not the greatest way to use JavaScript to identify a mobile device. Thus, we can also use JavaScript's window.orientation method to identify a mobile device by looking at the device's viewport's orientation. It gives us values in degrees, such as -90, 0, 90, and 180. Each of these variables represents a distinct viewpoint. The device is considered mobile if the viewport value is larger than 0; otherwise, it is not a mobile phone. NOTE: This functionality is no longer encouraged and is deprecated.Steps of the procedures Here are the steps to use JavaScript to detect a mobile device:
Example The following example shows whether the device being used is mobile or not. The device shows data information and an alert box for detecting a mobile device. Output The output shows the values for the detection of mobile devices. ![]() Method 3: Detect website is open on mobile deviceUsing Javascript, we can detect whether a website is open on a mobile device or not. We can use the "navigator.userAgent" method to detect whether a website works on mobile devices. We can use this method to detect mobile devices, tablets and desktops. Syntax The following JavaScript syntax is used to detect a mobile device for a website. Examples The following examples show whether the device being used is mobile or not. The device shows the Boolean values for detecting a mobile device. Example 1: The following example shows whether the website works on a mobile device. The device shows the Boolean values for detecting mobile devices. Output The output shows the Boolean values for the detection of mobile devices. ![]() Example 2: The following example shows whether the website is working on the mobile device or not. Output The output shows the Boolean values for the detection of mobile devices. ![]() Method 4: use the "navigator.vendor" methodThe navigator.vendor function detects possible mobile devices. We can see whether the device is mobile or not. Example: The following example shows whether the website is working on the mobile device or not. Output The output shows the Boolean values for the detection of mobile devices. ![]() |
We request you to subscribe our newsletter for upcoming updates.