Checking for missing parameter in function

前端 未结 4 1981
孤街浪徒
孤街浪徒 2021-02-01 16:57

Is this the correct way to check for a missing parameter in a function? Would this work in all browsers? How about IE?

function getName(name){
    name = name !=         


        
4条回答
  •  孤独总比滥情好
    2021-02-01 17:36

    The correct way to check is

    if (typeof name === "undefined") {
        // ...
    }
    

    Of course callers can still "fool" you by calling getName(undefined), when a parameter has been provided but the check will flag it as not-provided nonetheless. But that's really a pathological scenario.

提交回复
热议问题