Home  >  Article  >  Web Front-end  >  What are the basic rules for JavaScript parameters?

What are the basic rules for JavaScript parameters?

WBOY
WBOYforward
2023-08-22 17:13:16584browse

What are the basic rules for JavaScript parameters?

In this tutorial, let’s discuss the basic rules of JavaScript parameters.

JavaScript functions are codes that perform specific tasks. Function arguments are a list of names from the function definition. Parameters are also called formal parameters or formal parameters.

We can declare parameters according to the following syntax.

grammar

function functionName(param1, param2)
{
}

When a parameter has no value, the default value will be used.

rule

  • Declare default parameters as final parameters to avoid errors.

  • JavaScript function definition does not specify parameter types.

  • JavaScript functions do not check the type of input parameters.

  • JavaScript functions do not check the number of input parameters.

The Chinese translation of

Example 1

is:

Example 1

The following program declares the default parameters of a function. Therefore, when there are no parameter values, the function assumes the default 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>
The Chinese translation of

Example 2

is:

Example 2

The following program accepts an integer parameter and a string parameter input. The output returns 'NaN' because the function did not check the parameter types.

<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>
The Chinese translation of

Example 3

is:

Example 3

The following program has three parameters and four parameters. The length property of ‘arguments’ objects and functions differentiates them 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 teaches us the basic parameter rules. Be sure to declare a default parameter value to avoid errors. Pay attention to parameter types and parameter value types when coding. Be careful to follow the parameter order.

The above is the detailed content of What are the basic rules for JavaScript parameters?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete