Verify that a parameter has been supplied to the function

6

Assuming a JavaScript function with parameters:

function minhaFuncao (param1, param2, param3) {
    // fazer algo ...
}

And then using it as follows:

var param1 = "bubu";

minhaVariavel = minhaFuncao (param1);

or

var param1 = 30,
    param2 = true;

minhaFuncao (param1, param2);

Question

How can we check, within the function, if a parameter of any type was provided to avoid using it?

Example using the parameter without having received the same:

function minhaFuncao (param1) {

  alert("BuBu diz: " + param1);
}
minhaFuncao();

Note: It does not really matter the type of parameters, it is important to know if they were received to avoid problems in logic or to act accordingly.

    
asked by anonymous 06.04.2015 / 12:24

2 answers

4

Within each function you can check the typeof of each argument.

If typeof param1 == 'undefined' then it was not passed, or was passed with no set value ..

You can also check directly with:

if (param1 !== undefined) {
    // argumento enviado e não undefined
} else {
    // argumento não enviado ou undefined
}

JavaScript provides a arguments array inside the function with all the arguments that were passed to the function. You can check the arguments.length within the function to make sure the parameters have all been sent. Since it is not possible to skip parameters, that is to say minhaFuncao ('foo', , , 'bar'); arguments.length will give the number of arguments / parameters passed to the function. If the number of arguments is smaller than the parameter nr, you can find out which param- eters have not been passed (even if undefined ) to the function.

    
06.04.2015 / 13:02
4

If you want to check by type, then you can use the typeof operator.

  • typeof param1 == 'string'

  • typeof param2 == 'number'

  • typeof param3 == 'boolean'

var div = document.getElementById("xx");

function minhaFuncao(param1, param2, param3) {
  if (typeof param1 == 'string') {
    div.innerHTML += ("param1 é string") + " ";
  }
  if (typeof param2 == 'number') {
    div.innerHTML += ("param2 é number") + " ";
  }
  if (typeof param3 == 'boolean') {
    div.innerHTML += ("param3 é boolean") + " ";
  }
  div.innerHTML += "<br/>";
}

minhaFuncao();

minhaFuncao("");

minhaFuncao("", 2);

minhaFuncao("", 2, true);

minhaFuncao("", 2, true, "extra", "extra", "extra", "extra", "extra");
<div id="xx"></div>

It is possible to know which parameters have been passed based on the parameter count provided in the call.

To know how many parameters were given in the function call, you can use arguments.length .

So, if arguments.length is 1, then we know that param1 was provided. If it is 2 then param1 and param2 were provided. If 3, then all parameters were provided. If it is greater than 3, then the function was called with more parameters than the ones declared in its signature.

var div = document.getElementById("xx");

function minhaFuncao (param1, param2, param3) {
    div.innerHTML += "" +arguments.length + "<br/>" ;
}

minhaFuncao("");

minhaFuncao("", 2);

minhaFuncao("", 2, true);

minhaFuncao("", 2, true, "extra", "extra", "extra", "extra", "extra");
<div id="xx"></div>
    
06.04.2015 / 12:38