Check if a parameter has been provided for the function

Asked

Viewed 1,558 times

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 we can verify, within the function, whether a parameter of any type has been provided to avoid using the same ?

Example using the parameter without receiving the same:

function minhaFuncao (param1) {

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

Note: It does not matter the type of parameters, it matters whether they were received to avoid problems in logic or to act accordingly.

  • Can they be of these types or should they be of these types respectively? Or they can be any of the three but not if it is of another type, is considered without valid parameter?

  • @Bigown Edited to best illustrate the intended. The type is indifferent to the problem at hand.

  • I didn’t think it was so simple, it’s almost dup de http://answall.com/q/56710/101 which is already a dup.

  • 1

    @Bigown The subjects in the two you refer to are different from the one we find ourselves commenting on. Although the answers are quite complete and eventually refer to the solution to the problem we have in hand in this question, the others do not seek to know whether or not the parameter has been provided. In this seeks to set default value, in this also. The point is that looking for this particular problem I did not reach the questions/answers you referred to...

  • Yes, of course, that’s why I almost said. In this case you do what you want if you do not receive the parameter, in those what to do is already specified.

  • 1

    @bigown That’s it! I just clarified not to induce future readers to close as dup. They are related yes, but are not equal ;)

  • 3

    Who gave downvote, it would be good to know the reason to improve what has to improve or remove the question if it is not good for this site!

Show 2 more comments

2 answers

4

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

  • 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 were passed based on the count of parameters given in the call.

To know how many parameters were provided in the function call it is possible to use arguments.length.

Thus, if arguments.length for 1, so 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 those 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>

  • My goal is not exactly to know if 1, 2 or 3 parameters have been provided. The idea is to determine whether a parameter has been provided to avoid trying to use it and "blow up" the function. : ) (if you see that the question is not clear enough, let me know to rewrite it)

  • For example, if 2 parameters have been passed, then it means that the param3 was not past. If not this, then I did not understand the question.

  • I think I understand better... edited.

  • +1 for the examples and verification methods. I accepted @Sergio’s for having the solution check regardless of type.

4


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

If typeof param1 == 'undefined' then he was not passed over, or was passed over with no definite 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 an array arguments within the function with all arguments that have been passed to the function. You can check the arguments.length inside the function to make sure that all parameters have been sent.
Since it is not possible to skip parameters, ie do minhaFuncao ('foo', , , 'bar'); the arguments.length will give the number of arguments/parameters passed to the function. If the number of arguments is less than the parameter nr, it is possible to know which parameters were not passed (even if undefined) to the function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.