How to test if a parameter is provided to a function?

后端 未结 8 1055
不知归路
不知归路 2020-12-13 03:45

I was wondering, can you create a function with an optional parameter.

Example:

function parameterTest(test)
{
   if exists(test)
   {
     alert(\'t         


        
8条回答
  •  悲哀的现实
    2020-12-13 04:05

    null == undefined is true

    if (arg == null){
        // arg was not passed.
    }
    

    Code example:

    var button = document.querySelector("button");
    
    function myFunction(arg){
      if(arg == null){
        alert("argument was not passed.");
      } else {
        alert("argument " + arg + " was passed.");
      }
    }
    
    

提交回复
热议问题