Optional parameters in JavaScript
Function parameters can be made optional. To do this, the parameters must be set to their default values. For example, let's say we have the following function:
function func(num) {
console.log(num ** 2);
}
Let's make this parameter
default to 0:
function func(num = 0) {
console.log(num ** 2);
}
Let's check the work of our function with a parameter:
func(2); // shows 4
Let's check the work of our function without a parameter:
func(); // shows 0
Given a function:
function func(num = 5) {
console.log(num * num);
}
This function is invoked like this:
func(2);
func(3);
func();
Tell what will be the result of each of the function calls.
Given a function:
function func(num1 = 0, num2 = 0) {
console.log(num1 + num2);
}
This function is invoked like this:
func(2, 3);
func(3);
func();
Tell what will be the result of each of the function calls.