CuteMachine

How To Detect The Operating System In JavaScript

Sometimes we need to find out on which operating system our JavaScript code is running. We can easily detect the OS with the following functions.

function isWindows() {
  return navigator.appVersion.indexOf('Win') !== -1
}
function isMac() {
  return navigator.appVersion.indexOf('Mac') !== -1
}
function isLinux() {
  navigator.appVersion.indexOf('Linux')
}
function isUnix() {
  navigator.appVersion.indexOf('X11')
}

On my computer, this will print Mac :) in the console.

console.log(isMac() ? 'Mac :)' : 'no Mac :(')

Posted on CuteMachine in javascript.

Jo's Profile ImageWritten by Jo who lives and works in Frankfurt building digital doodah. Stalk him on Twitter.

TOS • Privacy • Disclaimer