Home > Article > Web Front-end > How to get default value in javascript
How to get default values in JavaScript
When writing JavaScript code, we usually set the parameter values of functions so that they can be passed to them when the function is called. However, in some cases we may want to make certain parameters have default values when not explicitly set, which can make the code more concise and easier to read. This article explains how to set and use default values in JavaScript.
Set default value
In the past, in order to set the default value of a function parameter, we usually needed to judge null or undefined in the function body, and then manually set a default value. As shown below:
function sayHello(name) { if(name === undefined) { name = "world"; } console.log("Hello, " + name + "!"); } sayHello(); // "Hello, world!" sayHello("Tom"); // "Hello, Tom!"
If we have multiple parameters that need to be set to default values, this approach can easily become verbose and difficult to maintain. In ES6, we can use a more convenient way to set default values.
Use default values
In ES6, we can specify default values for function parameters. This allows us to set the default value of the function more conveniently, while reducing the amount of code and improving readability. As shown below:
function sayHello(name="world") { console.log("Hello, " + name + "!"); } sayHello(); // "Hello, world!" sayHello("Tom"); // "Hello, Tom!"
In this example, we define a function sayHello and assign a default value of "world" to its parameter name. When the function is passed no parameters, it uses the default value and prints "Hello, world!". When we pass parameters to a function, it will use the value we pass, for example, when we pass Tom, it prints "Hello, Tom!".
We can also use the default values of other parameters to set the default value. For example:
function sayHello(name="world",greeting=`Hello`) { console.log(`${greeting}, ${name}!`); } sayHello(); // "Hello, world!" sayHello("Tom"); // "Hello, Tom!" sayHello("Jenny", "Hi"); // "Hi, Jenny!" sayHello(undefined, "Goodbye"); // "Goodbye, world!"
In this example, we define a function sayHello again and specify default values for its parameters name and greeting. When we don't pass the greeting parameter, it will use the default value "Hello". When we pass Tom and Hi, "Hi, Tom!" is output. When we pass undefined and "Goodbye", the default value will be used and "Goodbye, world!" will be output.
Notes on default values
When using default values, you need to pay attention to the following issues:
function getColor(color = "blue") { console.log(color); } getColor(null); // null getColor(""); // "" getColor(0); // 0
function sayHello(name = "world") { console.log(`Hello, ${arguments[0]}!`); } sayHello("Tom"); // "Hello, Tom!"
let language = "English"; function sayHello(name = "world", greeting = `Hello ${language}!`) { console.log(`${greeting}, ${name}!`); } sayHello(); // "Hello English, world!"
Conclusion
Before ES6, setting default values for function parameters was cumbersome and required manual judgment of null or undefined. The emergence of ES6 makes setting default values of function parameters more concise and easier to read. Setting default values can make the code more concise, increase maintainability, and reduce the occurrence of errors. In addition, we also need to note that when using default values, the parameters still exist, and the default values only apply to parameters that have not been set, and have nothing to do with other functions or global variables.
The above is the detailed content of How to get default value in javascript. For more information, please follow other related articles on the PHP Chinese website!