Checking for missing parameter in function

前端 未结 4 2046
孤街浪徒
孤街浪徒 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

    Yes this would work in all browsers though if you want to see if it is defined you might use:

    function getName(name){
       name = typeof(name) !== "undefined" ? name : "default";
       return name;
    }
    

提交回复
热议问题