Home > Web Front-end > JS Tutorial > body text

Understanding JavaScript, default parameters in functions

php中世界最好的语言
Release: 2017-11-18 09:33:05
Original
1697 people have browsed it

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();
Copy after login

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();
Copy after login

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();
Copy after login

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);
Copy after login

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);
Copy after login

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).

Understanding JavaScript, default parameters in functions

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();
Copy after login

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:


Understanding JavaScript, default parameters in functions

##Conclusion

JavaScript default parameters when writing a function very useful. The default parameters feature allows you to assign default values ​​to function parameters if they are missing when calling a function instead of defining them as undefined.

Related reading:

A brief introduction to foreach and each in JS


JS page refresh Summary of methods


The meaning and use of regular expressions in JavaScript


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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template