Default Parameters in JavaScript Functions
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.
1. Basic Syntax of Default Parameters
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:
- When
sum(5, 3)
is called, the result is8
becauseb
is explicitly passed as3
. - When
sum(5)
is called, the result is15
becauseb
is not passed, so it takes the default value of10
.
2. Using Default Parameters with Expressions
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.
3. Default Parameters with Destructuring
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.
Default Values in Object Destructuring
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:
- If the object is passed, the function uses the properties of that object.
- If no object is passed or the object is missing properties, the default values are used.
Default Values in Array Destructuring
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:
- The function works with an array and provides default values for
a
andb
if no values are passed.
4. Default Parameters and Function Arguments
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:
- The function multiplies
a
byb
, and ifb
is not provided, it defaults to2
.
5. Default Parameters with Functions that Return Defaults
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:
- If
name
is not provided, the functiongetDefaultName()
is called, and its result is used as the default value.
6. Default Parameters with Rest Parameters
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 joinStrings(separator = ",", ...strings) {
console.log(strings.join(separator));
}
joinStrings("-", "Hello", "World", "JavaScript"); // Output: Hello-World-JavaScript
joinStrings(" ", "Welcome", "to", "JavaScript"); // Output: Welcome to JavaScript
In this case:
- The
separator
parameter has a default value of,
if not provided, andstrings
will collect all additional arguments passed to the function.
Conclusion
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.
Top comments (0)