• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / User Details / Detect whether the page is being viewed on a mobile device or a desktop

Detect whether the page is being viewed on a mobile device or a desktop

Detect whether the page is being viewed on a mobile device or a desktop

Use a regular expression to test the navigator.userAgent property to figure out if the device is a mobile device or a desktop.

const detectDeviceType = () =>
  /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
    navigator.userAgent
  )
    ? 'Mobile'
    : 'Desktop';

// EXAMPLE
detectDeviceType(); // 'Mobile' or 'Desktop'

Source

https://www.30secondsofcode.org/js/s/detect-device-type

User Details

Related Snippets:

  • IP Grabber – get a users IP address with JavaScript
  • Detect the dark mode
  • Screen Size
  • Detect the preferred language of the current user

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers