Jordan Smalls

Software Engineer

Detect a mobile device

Check the user-agent string to see if a visitor is on a phone or tablet.

A single regex against navigator.userAgent covers the common mobile and tablet identifiers. This is a client-side heuristic, not a security check, and user agents can be spoofed or absent in some environments.

typescript
export function isMobileDevice(): boolean {
  if (typeof navigator === 'undefined') return false

  return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
    navigator.userAgent,
  )
}

Useful for swapping layouts or disabling hover-only interactions server-unaware. For anything layout-critical, prefer CSS media queries or a resize/matchMedia listener, since those respond to viewport rather than device claims.

typescript
if (isMobileDevice()) {
  // e.g. render a touch-friendly nav instead of hover dropdowns
}