Understand the default parameters in the JavaScript function


The JavaScript function can have the default parameter value. With the default function parameters, you can initialize a formal parameter with the default value. If you do not initialize parameters with certain values, the default value of this parameter is undefined.

 FUNCTION FOO (NUM1) {Console.log (Num1);} foo ();   
When the function foo is called, you did not pass any parameters, so the default value of the variable Num1 is set to undefined. However, sometimes you may need to set the default value rather than undefined. In the past, the best strategy was the test parameter value undefined and then assigned a value. So, in the above example, if you want to set the Num1’s default value to 9, you can do it in the way the following code:

Function Foo (Num1) {if (Num1 === undefined) {Num1 = 9;} console.log (Num1);} foo ();
ECMAScript 6 introduces a function The default parameter. With the default parameter feature of ECMA 2015, you will no longer need to check the undefined parameter values. Now you can set 9 as the default value of the parameter itself. You can rewrite the above functions to use the default, as shown below:

Function Foo (Num1 = 9) {Console.log (Num1);} foo ();
   For the function foo, if the value of the Num1 parameter is not passed, JavaScript will set 9 as the default value of NUM1. 
Check the undefined parameter

Even if you clearly pass the undefined as the parameter value when the function is called, the parameter value will be set to the default value.

FUNCTION FOO (Num1 = 9) {Console.log (Num1);} foo (undefined);

In the above code, you pass the value of undefined as NUM1; therefore, the value of Num1 will be set to the default 9.
  Runtime calculation default value  
JavaScript function default value is calculated at runtime. In order to better understand this, please see the following code:

Function Foo (Value = KOO ()) {Return Value;} Function Koo () {Return “Ignite UI “;} var A = foo (); console.log (a);
In the function foo, the default value of the parameter value is set to the function KOO. When the function foo is called at runtime, the function Koo is calculated. When you call the FOO function, you will get the output shown below (in this example, we use the Ignite UI framework).

Reuse default parameters
  The default parameter is used after the default parameter is used. Please see the following code:  
Function Foo (Num1 = 9, Num2 = Num1 + 8) {Console.log (Num2);} foo ();

Code on the aboveIn, use Num1’s default to calculate the default value of NUM2. The following output will be obtained when the function foo is called: 了解JavaScript函数中的默认参数

JavaScript default parameters are very useful when writing functions. When the function is called, if the parameters are missing, the default parameter function allows you to assign default values ​​for the function parameters, not to define it as undefined.

Easy Javascript Part 3: What is a default parameter in a Function?
The above is all the content of this article, I hope to help everyone, I hope everyone will support it. Tumi cloud.
© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment