-3
const browser = ((agent) => {
    switch (true) {
        case agent.indexOf("edge") > -1: return "edge";
        case agent.indexOf("edg") > -1: return "chromium based edge (dev or canary)";
        case agent.indexOf("opr") > -1 && !!window.opr: return "opera";
        case agent.indexOf("chrome") > -1 && !!window.chrome: return "chrome";
        case agent.indexOf("trident") > -1: return "ie";
        case agent.indexOf("firefox") > -1: return "firefox";
        case agent.indexOf("safari") > -1: return "safari";
    default: return "other";
    }
}) (window.navigator.userAgent.toLowerCase());

There is a simple example how to detect browser using navigator.userAgent property of Window object. Could someone explain what the latest line of this code actually does and why toLowerCase() method is necessary here?

Source

1 Answers1

1
}

End of the arrow function


)

End of the grouping operator that surrounds the arrow function


(...)

Calls the function, with arguments


window.navigator.userAgent.toLowerCase()

The argument


why toLowerCase() method is necessary here?

Because it is doing a case-insensitive comparison

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335