Detect the Operating System of User using JavaScript

JavaScript can detect a user's operating system to provide customized experiences, different interfaces, or platform-specific features. This capability is particularly useful for web applications that need to adapt their behavior based on the user's platform.

In this tutorial, we'll explore two primary methods for detecting the operating system using JavaScript:

  • The navigator.platform property

  • The navigator.userAgent property

Each method has its strengths and limitations. We'll examine both approaches with practical examples to help you choose the best solution for your needs.

Using navigator.platform Property

The navigator.platform property returns a string identifying the platform on which the browser is running. It provides a straightforward way to detect the operating system.

<script>
console.log("Platform:", navigator.platform);
</script>
Platform: MacIntel

Here's how to detect specific operating systems using navigator.platform:

<script>
function detectOS() {
    const platform = navigator.platform.toLowerCase();
    
    if (platform.indexOf("win") !== -1) {
        return "Windows";
    } else if (platform.indexOf("mac") !== -1) {
        return "Mac OS";
    } else if (platform.indexOf("linux") !== -1) {
        return "Linux";
    } else if (platform.indexOf("x11") !== -1) {
        return "Unix";
    } else {
        return "Unknown OS";
    }
}

const userOS = detectOS();
console.log("Operating System:", userOS);
document.getElementById("os-result").textContent = "Your OS: " + userOS;
</script>

<div id="os-result"></div>
Operating System: Mac OS
Your OS: Mac OS

Using navigator.userAgent Property

The navigator.userAgent property contains detailed information about the browser and operating system. It provides more comprehensive data but requires careful parsing.

<script>
console.log("User Agent:", navigator.userAgent);
</script>
User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

Here's a more robust OS detection function using navigator.userAgent:

<script>
function detectOSFromUserAgent() {
    const userAgent = navigator.userAgent.toLowerCase();
    
    if (userAgent.indexOf("windows nt") !== -1) {
        if (userAgent.indexOf("windows nt 10.0") !== -1) return "Windows 10/11";
        if (userAgent.indexOf("windows nt 6.3") !== -1) return "Windows 8.1";
        if (userAgent.indexOf("windows nt 6.1") !== -1) return "Windows 7";
        return "Windows";
    } else if (userAgent.indexOf("mac os x") !== -1) {
        return "Mac OS X";
    } else if (userAgent.indexOf("android") !== -1) {
        return "Android";
    } else if (userAgent.indexOf("iphone") !== -1 || userAgent.indexOf("ipad") !== -1) {
        return "iOS";
    } else if (userAgent.indexOf("linux") !== -1) {
        return "Linux";
    } else if (userAgent.indexOf("cros") !== -1) {
        return "Chrome OS";
    } else {
        return "Unknown OS";
    }
}

const detectedOS = detectOSFromUserAgent();
console.log("Detected OS:", detectedOS);
document.getElementById("detailed-os").textContent = "Detailed OS: " + detectedOS;
</script>

<div id="detailed-os"></div>
Detected OS: Mac OS X
Detailed OS: Mac OS X

Complete OS Detection Function

Here's a comprehensive function that combines both methods for better accuracy:

<script>
function getOperatingSystem() {
    const userAgent = navigator.userAgent.toLowerCase();
    const platform = navigator.platform.toLowerCase();
    
    // Check for mobile operating systems first
    if (userAgent.indexOf("android") !== -1) return "Android";
    if (userAgent.indexOf("iphone") !== -1 || userAgent.indexOf("ipad") !== -1) return "iOS";
    
    // Check for desktop operating systems
    if (platform.indexOf("win") !== -1 || userAgent.indexOf("windows") !== -1) {
        return "Windows";
    }
    if (platform.indexOf("mac") !== -1 || userAgent.indexOf("mac os") !== -1) {
        return "Mac OS";
    }
    if (platform.indexOf("linux") !== -1 || userAgent.indexOf("linux") !== -1) {
        return "Linux";
    }
    if (userAgent.indexOf("cros") !== -1) {
        return "Chrome OS";
    }
    
    return "Unknown";
}

// Display the result
const os = getOperatingSystem();
console.log("Your Operating System:", os);

// You can use this information for conditional logic
switch(os) {
    case "Windows":
        console.log("Showing Windows-specific features");
        break;
    case "Mac OS":
        console.log("Showing Mac-specific features");
        break;
    case "Android":
    case "iOS":
        console.log("Showing mobile-optimized interface");
        break;
    default:
        console.log("Showing default interface");
}
</script>
Your Operating System: Mac OS
Showing Mac-specific features

Comparison

Method Accuracy Detail Level Mobile Support
navigator.platform Good Basic Limited
navigator.userAgent Better Detailed Excellent
Combined approach Best Comprehensive Excellent

Key Points

  • navigator.platform is simpler but less detailed

  • navigator.userAgent provides more information including mobile devices

  • Both properties can be spoofed by users or browsers

  • Always include fallback cases for unknown operating systems

  • Consider using feature detection instead of OS detection when possible

Conclusion

JavaScript provides effective methods to detect user operating systems through navigator.platform and navigator.userAgent. The combined approach offers the most reliable detection, covering both desktop and mobile platforms for better user experience customization.

Updated on: 2026-03-15T23:19:01+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements