0

I'm writing an app in Javascript, and while I'm comfortable with the language, I was wondering about unused parameters.

Say I have a function as follows:

function test(data, complete){
    if (data){
        return complete(null, 'yes');
    }
    else{
        return complete('error', null);
    }
}

I call that function with a callback, but am only interested in checking for an error - if the data exists, I can move forward with my program. Is it okay if I just pass the error parameter into the function?

test(data, function(err){
    if(err){
        //Uh Oh
    }
    else{
        //Keep going
    }
});

Or is it best if I pass both the error and the result (even though the result variable remains unused)?

test(data, function(err, result){
    if(err){
        //Uh Oh
    }
    else{
        //Keep going
    }
});

2 Answers 2

4

Is it okay if I just pass the error parameter into the function?

Yes. Arguments that don't receive an explicit value will receive undefined instead (but will still exist and be locally scoped so will still mask variables with the same name from a wider scope).

As an aside, any extra arguments will still be accessible through the array-like arguments object.

Sign up to request clarification or add additional context in comments.

Comments

2

It doesn't matter.

Function arguments are only used to create local variables.
You can pass any number of parameters to any function, no matter how its declared.

1 Comment

You pass arguments into a function not parameters ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.