简体   繁体   中英

JavaScript arrow-function using navigator.userAgent as an example

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

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM