Answer: Read the browser's navigator object, mainly navigator.userAgent (or navigator.platform), and check it for OS names.
let ua = navigator.userAgent;
let os = "Unknown OS";
if (ua.indexOf("Win") !== -1) os = "Windows";
else if (ua.indexOf("Android") !== -1) os = "Android";
else if (/iPhone|iPad|iPod/.test(ua)) os = "iOS";
else if (ua.indexOf("Mac") !== -1) os = "macOS";
else if (ua.indexOf("Linux") !== -1) os = "Linux";
console.log(os);
The user-agent string describes the browser and operating system, so searching it tells you the client's OS. Android is checked before Linux and iOS before Mac because those user-agent strings also contain "Linux" and "Mac".
Keep in mind that the user agent can be changed by the user, so this detection is approximate. Newer Chromium browsers also offer navigator.userAgentData.platform.