4

How do I check that a function has received no arguments? For example, I want to be able to create a custom function which accepts multiple inputs like so:

clear();      // clear all
clear('a');   // clear a
clear('b');   // clear b
clear('c');   // clear c
clear('d');   // clear d

1 Answer 1

7

You may either check if an argument is undefined:

function clear(variable) {
    if (variable === undefined) { ... }
}

or simply check the number of arguments:

function clear(variable) {
    if (arguments.length === 0) { ... }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Is one more effective or efficient than the other?
there's a minor bit of nuanced difference between the two. clear(undefined) will clear all in the first, but not in the second.
@JaPerk14 Agree with zzzzBov here. The first will work unless you need to pass undefined as an argument. Otherwise both variants are same good. Maybe the second is slightly more foolproof.

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.