Get user OS without deprecated and with all browser’s support

A simple and current way to get the user's OS with Javascript with support for all browsers

Get user OS without deprecated and with all browser’s support
Photo by Arian Darvishi / Unsplash

If you need get the OS of user with Javascript you may have noticed by now that many of the APIs that exist in web posts are deprecated.
APIs like navigator.plataform or navigator.appVersion for exemple are deprecated, and others like navigator.userAgentData.platform don't support all browsers (like Firefox, Safari and others):

Browser suport for navigator.userAgent

So, an alternative for this caos is use navigator.userAgent and search in the returned string for OS:

const isMacos = navigator.userAgent.indexOf('Mac') != -1
const isWindows = navigator.userAgent.indexOf('Win') != -1

You can think a smart way to get the OS too, like using Regex or some like that, this is just a simple example.
It's not so confiable information too, so if you need to be sure about which OS this is not the ideal way:

Navigator: userAgent property - Web APIs | MDN (mozilla.org)