Calling javascript function with null parameter

Asked

Viewed 1,328 times

1

The function verificaCampoVazios has two parameters (fs and campos).

verificaCamposVazios = function (fs, campos)
{
 console.log(campos[0]);
}

Example of how it is called function:

verificaCamposVazios(fsInformacaoCandidatoDados, ArrayCamposNaoObrigatorios);

How do I call the function without passing the parameter campos?

I called the function so:

verificaCamposVazios(fsInformacaoCandidatoDados);

And the function was not executed, but did not show error in console.log of Chrome.

I would like to understand what I call the function without passing an argument.

  • Have you tried verificaCamposVazios(fsInformacaoCandidatoDados, null);?

  • 1

    I’ll try now

  • 2

    It is OK to call the function with less arguments... -> https://jsfiddle.net/7dnc5kh0/ you can create a jsFiddle that plays the problem?

  • I thought that when the argument was not passed, the Function argument was already null

  • with null gives error too.

2 answers

3


You can use the null as an argument:

verificaCamposVazios(fsInformacaoCandidatoDados, null);

But normally the lack of this should be no problem at all, because the omitted value will be null anyway.

In the specific case of the question, it may be a characteristic of the function itself to react differently based on the number of parameters, based on the function arguments.length.

For example, the called function may have something like this implemented:

if( arguments.length < 2 ) {
   ... retorna sem fazer nada ...

what would be a specificity of the function itself, and not of the null proper.

  • In the function there is a condition that reads the parameter and makes a comparison. Can the error be this? As there is no parameter, the function goes crazy.

  • 2

    I don’t think so, because null would be the same (unless the comparison is with Undefined, as Pedro commented). Is there no way you can play the function in Codepen, Jsfiddle, or somewhere? Then it would be easier to analyze.

  • look at the edition I made in the question. Now you will understand that the function tries to read something that does not exist.

  • 1

    You are using index. Then you will give problem calling with null anyway, it is not what you asked initially. The problem is independent of passing null as parameter or not. It would be the case to apply a default value in case it comes empty.

  • You are correct, and you have already predicted this in your reply

  • 1

    @durtto both Pedro’s answer and mine have important information, is that without seeing the function, we are based on theories. depending on what you do, the null is important, depending on the case may be better Undefined; In the next questions look for as many details, then we can help you better.

  • Yes. Unfortunately the mistake was right where I didn’t show it. This happens, and I realized that your answer dealt precisely with this passage that I did not show. I am very grateful. It helped me a lot because my work was stopped. Thank you for the humility of both of you and attention.

Show 2 more comments

2

You can call a function with missing parameters without any problem, including it is a practice (as far as I know) for optional parameters, the only thing you should do to avoid errors, is to check if the parameter is not undefined within its function.

verificaCamposVazios = function (fs, campos)
{
    if (campos !== undefined) {
        console.log(campos[0]);
    }    
}

To call the function, you will not need to pass the second parameter or pass it as null.

verificaCamposVazios(fsInformacaoCandidatoDados);
  • In the function there is a condition that reads the parameter and makes a comparison. Can the error be this? As there is no parameter, the function goes crazy.

  • I believe so, this parameter will be undefined, tries to check the value before using the parameter.

  • You’re right. I did so. if (Fs == null || fields == null) { Return -1 }

  • Note that null is different from undefined. If you pass nothing in the second parameter it will be undefined and not null

  • 1

    Thanks Peter for the help.

Browser other questions tagged

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