function multiply(a, b = 1) {
return a * b;
}
function read_file(file, default_param = false) {
// Code
}
function greeting (name = 'stranger') {
console.log(`Hello, ${name}!`)
}
greeting('Nick') // Output: Hello, Nick!
greeting() // Output: Hello, stranger!
function sum(x = 3, y = 5) {
// return sum
return x + y;
}
console.log(sum(5, 15)); // 20
console.log(sum(7)); // 12
console.log(sum()); // 8
function sum(x = 1, y = x, z = x + y) {
console.log( x + y + z );
}
sum(); // 4
// Default Parameter Values in javascript
// Longhand:
function volume(l, w, h) {
if (w === undefined)
w = 3;
if (h === undefined)
h = 4;
return l * w * h;
}
console.log(volume(2)); //output: 24
// Shorthand:
volume_ = (l, w = 3, h = 4 ) => (l * w * h);
console.log(volume_(2)) //output: 24
// using a function in default value expression
const sum = () => 15;
const calculate = function( x, y = x * sum() ) {
return x + y;
}
const result = calculate(10);
console.log(result); // 160