What are the basic rules for JavaScript parameters?

In this tutorial, let us discuss the basic rules for JavaScript parameters.

A JavaScript function is a code that performs a particular task. The function parameters are the name list in the function definition. Parameters are also known as formal parameters or formal arguments.

We can follow the syntax below to declare parameters.


function functionName(param1, param2)

When there is no value for a parameter, a default value comes in place.


  • Declare the default parameters as the final ones to avoid an error.

  • The JavaScript function definition does not specify a parameter type.

  • The JavaScript function does not check the input argument type.

  • The JavaScript function does not check the number of input arguments.

Example 1

The program below declares a default argument to the function. Therefore, the function takes the default value when there is no argument value.

<html> <body> <h2>Default parameters in JavaScript </h2> <p> <b> Input = Sum(10) </b> </p> <p> <b> Input = Sum(10,30) </b> </p> <div id="paramDefBtnWrap"> <button id="paramDefBtn"> Run </button> </div> <p id="paramDefOut"> </p> <script> var paramDefOut = document.getElementById("paramDefOut"); var paramDefBtnWrap = document.getElementById("paramDefBtnWrap"); var paramDefBtn = document.getElementById("paramDefBtn"); var paramDefInpStr = ""; paramDefBtn.onclick = function() { //paramDefBtnWrap.style.display = "none"; function sum(param1, param2 = 20) { var sum = param1 + param2; paramDefInpStr += param1 + " + " + param2 + " = " + sum + "<br><br>"; } sum(10); sum(10, 30); paramDefOut.innerHTML = paramDefInpStr; }; </script> </body> </html>

Example 2

The program below gets one integer argument and one string argument input. The output returns ‘NaN’ because the function does not check the parameter type.

<html> <body> <h2> Illustrating parameter type and argument type</h2> <p> <b> Input = Multiply(10, 'ten') </b> </p> <div id="paramArgBtnWrap"> <button id="paramArgBtn"> Run </button> </div> <p id="paramArgOut"> </p> <script> var paramArgOut = document.getElementById("paramArgOut"); var paramArgBtnWrap = document.getElementById("paramArgBtnWrap"); var paramArgBtn = document.getElementById("paramArgBtn"); var paramArgInpStr = ""; paramArgBtn.onclick = function() { //paramArgBtnWrap.style.display = "none"; function mult(param1, param2) { var prod = param1 * param2; paramArgInpStr += "Argument 1 type is " + typeof(param1) + "<br><br>"; paramArgInpStr += "Argument 2 type is " + typeof(param2) + "<br><br>"; paramArgInpStr += param1 + " × " + param2 + " = " + prod + "<br><br>"; } mult(10, 'ten'); paramArgOut.innerHTML = paramArgInpStr; }; </script> </body> </html>

Example 3

The program below has three parameters and four arguments. The ‘arguments’ object and the function length property differentiate it for us.

<html> <body> <h2> Illustrating number of arguments and parameters in a function</h2> <p> <b> Input = 1, 'one', Date, {} </b> </p> <div id="infArgBtnWrap"> <button id="infArgBtn"> Run </button> </div> <p id="infArgOut"> </p> <script> var infArgOut = document.getElementById("infArgOut"); var infArgBtnWrap = document.getElementById("infArgBtnWrap"); var infArgBtn = document.getElementById("infArgBtn"); var infArgInpStr = ""; infArgBtn.onclick = function() { //infArgBtnWrap.style.display = "none"; function argFunction(arg1, arg2, arg3) { infArgInpStr += "Function Parameters = " + argFunction.length + '<br><br>'; infArgInpStr += "Function Arguments = " + arguments.length + '<br><br>'; infArgInpStr += "Arguments are, <br><br>" for (i = 0; i < arguments.length; i++) { infArgInpStr += argFunction.arguments[i] + "<br>"; } infArgOut.innerHTML = infArgInpStr; } argFunction(1, 'one', Date, {}); }; </script> </body> </html>

This tutorial taught us the basic parameter rules. Be sure to declare a default parameter value to avoid an error. Have an awareness of parameter type and argument type while coding. Be careful to follow the parameter order.