41

I was trying to write some javascript function and realised that

function testFunction(input_1, input_2, input_3) {
 alert("alert");
}

however when i call the function like this:

<input type="button" value="click" onclick="testFunction("1", "2")">

why will it still work even with just two parameters?

James Allardice
166k22 gold badges335 silver badges316 bronze badges
asked Jun 22, 2011 at 20:41
3
  • 1
    Because javascript is dynamic? It doesn't have types, it doesn't force compliance for parameters. Commented Jun 22, 2011 at 20:43
  • 3
    onclick="testFunction("1", "2")"> should be onclick="testFunction('1', '2')">. Otherwise you'll get errors. Commented Jun 22, 2011 at 20:44
  • 16
    Exciting question - you can call a function with less, more or no parameters. And you can even access arguments that aren't parameters. The 4th parameter in testFunction('1', '2', '3', '4') can even be accessed inside the function using arguments[3]; +1 to you my friend. Commented Jun 22, 2011 at 20:44

8 Answers 8

43

You can call a Javascript function with any number of parameters, regardless of the function's definition.

Any named parameters that weren't passed will be undefined.

Extra parameters can be accessed through the arguments array-like object.

answered Jun 22, 2011 at 20:43
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your reply but what if i have several parameters and yet some in the middle are going to be empty, is there a way which i can path the correct parameters to the correct receiving ends of the function?
@sim: Just pass null or false or any other value.
@simplified.: No, arguments will always be assigned to parameters in order. If you want to "skip" one you have to pass another value, like null.
4

It doesn't actually matter how many parameters you are providing. the function interprets them and creates the arguments object (which acts as an array of parameters). Here's an example:

function sum(){
 if(arguments.length === 0)
 return 0;
 if(arguments.length === 1)
 return arguments[0];
 return arguments[0] + sum.apply(this, [].slice.call(arguments, 1));
}

It's not the most efficient solution, but it provides a short peak at how functions can handle arguments.

answered Jun 22, 2011 at 20:44

Comments

2

Because javascript treats your parameters as an array; if you never go beyond the second item, it never notices an argument is missing.

answered Jun 22, 2011 at 20:43

1 Comment

More specifically it's the arguments array. And named parameters in function declarations are just pointers to members of arguments
2

Javascript does not support Method overloading hence method will be execute in order of their occurence irrelevant of arguments passed Because javascript has no type checking on arguments or required qty of arguments, you can just have one implementation of testFunction() that can adapt to what arguments were passed to it by checking the type, presence or quantity of arguments..

answered May 20, 2019 at 6:14

Comments

1

The third parameter can be optional and will have a (削除) null (削除ここまで) undefined default value.

If you explicitly want to have a required parameter, you need to require it via code inside the function.

answered Jun 22, 2011 at 20:43

5 Comments

Close, the third parameter will be undefined.
@Ahman-San thanks for the reply, but what's the actual way for explicitly making the parameter complusory?
@simplified.: By testing whether it is undefined and throwing an error. Depending on the acceptable values, you might able to just do if(parameter).
Many ways. One way is you could implement a condition inside the function to check for the values of each parameter and if any of the parameters is undefined then the return something that tells you that some data is missing. Otherwise you can just ignore the rest of the function and exit it immediately
@simplified - to expand slightly on what Felix and Ahmad-San said, there's no way to make parameters compulsory in the sense of using some keyword to have JavaScript automatically throw an error, hence the need to code your own test within the function as needed.
0
answered Jun 22, 2011 at 20:43

Comments

0

Javascript is a very dynamic language and will assume a value of "undefined" for any parameters not passed a value.

answered Jun 22, 2011 at 20:43

Comments

0

Try using the below code

var CVH = {
 createFunction: function (validationFunction, extParamData, extParamData1) {
 var originalFunction = validationFunction;
 var extParam = extParamData;
 var extParam1 = extParamData1;
 return function (src, args) {
 // Proxy the call...
 return originalFunction(src, args, extParam, extParam1);
 }
 }
 }
function testFunction(input_1, input_2, input_3) {
 alert("alert");
}

and you can call this function as below

<input type="button" value="click" onclick="CVH.createFunction(testFunction('1', '2'),'3','4')">
answered Apr 12, 2019 at 10:52

2 Comments

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
sorry but this answer is not relevant, i misunderstood the question

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.