Developer Guide

How to Detect OS with JavaScript

A production-ready guide to detecting Windows, macOS, Linux, iOS, and Android using modern JavaScript — with Client Hints, UA parsing, and platform detection.

How do I detect the operating system in JavaScript?

The most reliable modern method is navigator.userAgentData.platform (available in Chrome/Edge 90+). For broader support, parse navigator.userAgent for keywords like 'Windows', 'Mac OS X', 'Linux', 'Android', 'iPhone', 'iPad'. Alternatively, navigator.platform provides a simpler but less detailed value. Always combine multiple signals for accuracy.

Method 1: User-Agent Client Hints (Modern)

The navigator.userAgentData API provides structured OS information without parsing strings. Available in Chrome 90+, Edge 90+, and Opera 76+. It's the recommended approach for Chromium-based browsers.

Method 2: User Agent String Parsing

For cross-browser compatibility, parsing navigator.userAgent works everywhere. Here's a comprehensive OS detection function:

Production-Ready Combined Approach

For production applications, combine Client Hints with UA fallback for maximum browser coverage:

Best Practices for OS Detection

Use OS detection for UX, not functionality. Show platform-specific download links or keyboard shortcuts, but don't gate features behind OS detection.

Handle iPadOS carefully. iPadOS 13+ masquerades as macOS. Always check navigator.maxTouchPoints alongside the UA string.

Windows 10 vs 11 requires Client Hints. The UA string shows "Windows NT 10.0" for both. Only platformVersion via Client Hints can distinguish them.

Linux distributions can't be detected. The UA string shows "Linux x86_64" for Ubuntu, Fedora, Arch, etc. Distribution detection is not possible through the browser.

Test with real user agent strings. Use our Common User Agent Strings reference for testing.

Frequently Asked Questions

How do I detect the operating system in JavaScript?

The most reliable modern method is navigator.userAgentData.platform (available in Chrome/Edge 90+). For broader support, parse navigator.userAgent for keywords like 'Windows', 'Mac OS X', 'Linux', 'Android', 'iPhone', 'iPad'. Alternatively, navigator.platform provides a simpler but less detailed value. Always combine multiple signals for accuracy.

Can I detect the OS version with JavaScript?

Yes, but with limitations. On Windows, the UA string includes version info like 'Windows NT 10.0'. On macOS, look for 'Mac OS X 10_15_7'. On Android, the version appears as 'Android 14'. iOS version is in the UA as 'iPhone OS 17_0'. User-Agent Client Hints' platformVersion provides more structured version data on Chromium browsers.

What is the difference between navigator.platform and navigator.userAgent for OS detection?

navigator.platform returns a simple string like 'Win32', 'MacIntel', 'Linux x86_64', or 'iPhone'. It's simpler but less detailed — it doesn't include the OS version. navigator.userAgent contains the full UA string with OS version, browser info, and more. navigator.platform is deprecated but still widely supported.

How do I detect if a user is on a Mac with Apple Silicon?

Detecting Apple Silicon (M1/M2/M3) vs Intel Mac is tricky. navigator.platform returns 'MacIntel' for both. User-Agent Client Hints can reveal architecture ('arm' vs 'x86'). You can also use WebGL to check the GPU renderer — Apple Silicon Macs report 'Apple M1 GPU' or similar. WebAssembly feature detection can also differentiate ARM vs x86.

How do I detect Chrome OS?

Chrome OS is identified by the presence of 'CrOS' in the user agent string. For example: 'CrOS x86_64 14541.0.0'. You can check navigator.userAgent.includes('CrOS'). Chrome OS also supports navigator.userAgentData where the platform will be 'Chrome OS'.

Does OS detection work reliably in all browsers?

OS detection via user agent parsing works in all browsers but accuracy varies. Browsers can spoof their UA string (e.g., request desktop site on mobile). User-Agent Client Hints provide more reliable data but are only available in Chromium browsers. For critical decisions, combine UA parsing with feature detection and Client Hints.