Function parameters and return values
Function parameters and return values are an important aspect of functions in JavaScript.
Function Parameters: Function parameters are the variables defined in the function signature that receive the values passed to the function when it's called. You can specify any number of parameters in a function, separated by commas. If a function is called with a different number of arguments than the parameters defined in the function, the additional or missing values will be undefined.
For example:
function add(x, y) {
return x + y;
}
console.log(add(1, 2)); // Output: 3
In the above example, the function add takes two parameters x and y.
Default parameters are a feature of JavaScript functions that allow you to specify default values for parameters that are not passed to the function when it's called. This is useful in situations where you want to provide a default behavior for a function if no value is specified for a particular parameter.
Here's an example of a function with a default parameter:
javascriptCopy codefunction greet(name = "User") {
console.log(`Hello, ${name}!`);
}
greet(); // Output: Hello, User!
greet("John"); // Output: Hello, John!
In this example, the function greet takes a single parameter name, and it has a default value of "User". If you call the function without passing an argument, the default value will be used. If you call the function with an argument, the value of the argument will be used instead of the default.
It's important to note that default parameters are evaluated only once when the function is defined, and not each time the function is called. This means that if you pass an object as the default value for a parameter, any changes to the object will persist between calls to the function.
Function Return Values: The return value of a function is the value that is returned to the caller when the function is done executing. Functions can return any type of value, including primitive types, objects, arrays, or other functions.
For example:
function multiply(x, y) {
return x * y;
}
console.log(multiply(2, 3)); // Output: 6
In the above example, the function multiply takes two parameters x and y, and returns their product.
If a function doesn't explicitly return a value, it returns undefined by default. You can also use the return statement to exit a function early, before it has executed all its code.
It's important to note that the return value of a function should always match the function signature, which specifies the type of value that the function returns. The function signature is used by the JavaScript engine to optimize the code and ensure that the function is used correctly.
A rest parameter is a feature of JavaScript functions that allows you to represent an indefinite number of arguments as an array. It is specified using three dots (...) followed by the parameter name. The rest parameter must be the last parameter in the function signature.
Here's an example of a function with a rest parameter:
function sum(...numbers) {
let result = 0;
for (let num of numbers) {
result += num;
}
return result;
}
console.log(sum(1, 2, 3, 4)); // Output: 10
In this example, the function sum takes an indefinite number of arguments and adds them together. The rest parameter numbers is an array that contains all the arguments passed to the function.
Summing up the topic
Function parameters are variables defined in a function's signature to receive arguments when the function is called. Return values are the values a function returns to the caller. JavaScript also has features such as rest parameters, spread operators, and default values for parameters to handle arguments more efficiently. The return statement specifies the value to be returned by a function.
- What is a function parameter in JavaScript?
- What is a return value in JavaScript?
- Can a function have multiple parameters in JavaScript?
- What happens if a function does not have an explicit
returnstatement in JavaScript?
Hands-on exercises
- Create a function that calculates the average of three numbers. The function should accept three parameters and return the average.
- Create a function that calculates the factorial of a number. The function should accept the number as a parameter and return the factorial.
