JavaScript - Default Parameters in JavaScript
1. Default Parameters in JavaScript
Default parameters allow you to set a default value for a function parameter.
If an argument is not provided or is undefined, JavaScript uses the default value.
Syntax
function functionName(parameter = defaultValue) {
// Code to execute
}
Example 1: Basic Default Parameter
function greet(name = "Guest") {
console.log(`Hello, ${name}!`);
}
greet("Alice"); // Argument provided
greet(); // No argument provided
Output:
Hello, Alice!
Hello, Guest!
-
If you don’t pass
name
, it defaults to "Guest".
Example 2: Multiple Default Parameters
function introduce(name = "Anonymous", age = 18) {
console.log(`${name} is ${age} years old.`);
}
introduce("Bob", 25); // Both arguments passed
introduce("Emma"); // Only name passed
introduce(); // No arguments passed
Output:
Bob is 25 years old.
Emma is 18 years old.
Anonymous is 18 years old.
Example 3: Default Parameters with Expressions
You can set dynamic default values:
function calculatePrice(price, tax = price * 0.1) {
return price + tax;
}
console.log(calculatePrice(100)); // Uses default tax
console.log(calculatePrice(200, 50)); // Uses custom tax
Output:
110
250
Example 4: Default Parameter vs undefined
If you pass undefined
, the default value is used.
But if you pass null
, the default won’t be used.
function show(message = "Hello") {
console.log(message);
}
show(); // "Hello"
show(undefined); // "Hello"
show(null); // null
show("Hi!"); // "Hi!"
Output:
Hello
Hello
null
Hi!
2. Return Values in JavaScript ✅
A return value is the data a function sends back when it finishes execution.
Syntax
function functionName(parameters) {
return value;
}
-
return
stops function execution immediately. -
Without
return
, the function returnsundefined
.
Example 1: Returning a Value
function add(a, b) {
return a + b;
}
const result = add(5, 10);
console.log(result);
Output:
15
-
The
return
statement sends the result back to the caller.
Example 2: Function Without Return
function multiply(a, b) {
console.log(a * b);
}
const result = multiply(5, 4);
console.log(result);
Output:
20
undefined
-
No
return
→ function returnsundefined
.
Example 3: Using return
to Exit Early
function checkAge(age) {
if (age < 18) {
return "Access Denied";
}
return "Access Granted";
}
console.log(checkAge(16));
console.log(checkAge(20));
Output:
Access Denied
Access Granted
-
Once
return
runs, the function stops executing.
Example 4: Returning Multiple Values
JavaScript can only return one value, but we can use arrays or objects:
Using Arrays
function getCoordinates() {
return [40.7128, -74.006];
}
const [lat, lng] = getCoordinates();
console.log(lat, lng);
Output:
40.7128 -74.006
Using Objects
function getUser() {
return { name: "Alice", age: 25 };
}
const { name, age } = getUser();
console.log(name, age);
Output:
Alice 25
3. Combining Default Parameters + Return Values
function calculateBill(amount, tax = 0.1, tip = 0.05) {
return amount + amount * tax + amount * tip;
}
console.log(calculateBill(100)); // Uses default tax & tip
console.log(calculateBill(100, 0.2)); // Custom tax, default tip
console.log(calculateBill(100, 0.2, 0)); // Custom tax, no tip
Output:
115
125
120
4. Quick Summary
Feature | Default Parameters | Return Values |
---|---|---|
Definition | Assigns a default value if no argument is provided | Sends data back from a function |
Introduced In | ES6 | Always existed |
When Used | When arguments might be missing | When you need a function to give a result |
Returns undefined |
If argument = undefined , default used |
If no return provided |
Use Case | Optional parameters | Reusable calculations, passing data |