Search code examples
javascriptuser-agentindexofarrow-functionsnavigator

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


Solution

  • }
    

    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