Default parameters in JavaScript allow you to specify default values for function parameters. If the function is called without a value for a particular parameter, the default value is used instead.
This feature was introduced in ES6 (ECMAScript 2015) and helps to handle cases where a parameter may not be provided, avoiding undefined behavior and making your code more robust.
In the following example, if no argument is passed for b, it will default to 10:
function sum(a, b = 10) { return a + b; } console.log(sum(5, 3)); // Output: 8 console.log(sum(5)); // Output: 15 (b defaults to 10)
In this case:
You can also set default values that are the result of expressions, such as calling a function or performing calculations.
function greet(name = "Guest") { console.log(`Hello, ${name}!`); } greet(); // Output: Hello, Guest! greet("John"); // Output: Hello, John!
Here, "Guest" is the default value for name if no value is passed to the function.
Default parameters also work with destructuring. If you are using object or array destructuring in the function parameters, you can set default values for specific properties or items.
function printInfo({ name = "Anonymous", age = 18 } = {}) { console.log(`Name: ${name}, Age: ${age}`); } printInfo({ name: "Alice", age: 25 }); // Output: Name: Alice, Age: 25 printInfo({}); // Output: Name: Anonymous, Age: 18 printInfo(); // Output: Name: Anonymous, Age: 18
In this case:
function printNumbers([a = 1, b = 2] = []) { console.log(`a: ${a}, b: ${b}`); } printNumbers([5, 10]); // Output: a: 5, b: 10 printNumbers([7]); // Output: a: 7, b: 2 printNumbers(); // Output: a: 1, b: 2
In this case:
You can also use default parameters in conjunction with arguments to handle cases where the number of arguments is dynamic.
function multiply(a, b = 2) { console.log(a * b); } multiply(3, 4); // Output: 12 multiply(3); // Output: 6 (b defaults to 2)
In this example:
You can use functions as default values, which can be helpful in scenarios where you need dynamic or complex default values.
function getUser(id, name = getDefaultName()) { console.log(`User ID: ${id}, Name: ${name}`); } function getDefaultName() { return "John Doe"; } getUser(1); // Output: User ID: 1, Name: John Doe getUser(2, "Alice"); // Output: User ID: 2, Name: Alice
In this example:
You can combine default parameters with rest parameters (...args) to handle scenarios where you don't know how many arguments a function might receive.
function sum(a, b = 10) { return a + b; } console.log(sum(5, 3)); // Output: 8 console.log(sum(5)); // Output: 15 (b defaults to 10)
In this case:
Default parameters in JavaScript make functions more flexible by allowing you to define default values for parameters that are missing or undefined. This feature improves code readability and helps prevent errors caused by missing arguments.
It can be combined with other features such as destructuring, rest parameters, and even function calls to create dynamic, concise, and maintainable functions.
Hi, I'm Abhay Singh Kathayat!
I am a full-stack developer with expertise in both front-end and back-end technologies. I work with a variety of programming languages and frameworks to build efficient, scalable, and user-friendly applications.
Feel free to reach out to me at my business email: kaashshorts28@gmail.com.
The above is the detailed content of Understanding Default Parameters in JavaScript Functions. For more information, please follow other related articles on the PHP Chinese website!