Our study or work will involve JavaScript, so we must also understand JavaScript. Functions are a large part of JavaScript. And functions are ever-changing. This article will help you understand and master JavaScript. Let's study it carefully.
JavaScriptFunction, can have default parameter values. By defaulting function parameters you can initialize formal parameters with default values. If a parameter with a certain value is not initialized, the parameter's default value is undefined.
Please look at the following code:
function foo(num1){ console.log(num1); } foo();
When calling function foo, you do not pass any parameters, so the default value of variable num1 is set to undefined. However, sometimes you may want to set a default value instead of undefined. In the past, the best strategy was to test the parameter value to undefined and then assign a value. So, in the above example, if you want to set the default value of num1 to 9, then you can do it as shown in the following code:
function foo(num1) { if (num1 === undefined) { num1 = 9; } console.log(num1); } foo();
ECMAScript 6 introduced default parameters for functions. With ECMA 2015's default parameters feature, you no longer need to check for undefined parameter values. Now, you can set 9 as the default value for the parameter itself. You can rewrite the above function to use the default value as follows:
function foo(num1 =9) { console.log(num1); } foo();
For function foo, if the value of the num1 parameter is not passed, then JavaScript will set 9 as the default for num1 value.
Check for undefined parameters
Even if you explicitly pass undefined as the parameter value when calling the function, the parameter value will be set to the default value.
function foo(num1 =9) { console.log(num1); } foo(undefined);
In the above code, you are passing undefined as the value of num1; therefore, the value of num1 will be set to the default value of 9.
Computed default value at runtime
JavaScript function default value is calculated at runtime. To understand this better, take a look at the following code:
function foo(value = koo()) { return value; } function koo() { return "Ignite UI"; } var a = foo(); console.log(a);
In function foo, the default value of the parameter value is set to function koo. When function foo is called at runtime, function koo is evaluated. After calling the foo function, you will get the output as shown below (in this example, we used the Ignite UIframework).
Reuse default parameters
The default parameters can be used by subsequent default parameters. Please look at the following code:
function foo(num1 = 9, num2 = num1 + 8){ console.log(num2); } foo();
In the above code, the default value of num1 is used to calculate the default value of num2. When calling function foo you will get the following output:
The above is the detailed content of Understanding JavaScript, default parameters in functions. For more information, please follow other related articles on the PHP Chinese website!