JavaScript functions are a set of statements that perform a specific task. They are used to break down a large program into smaller, more manageable pieces. Functions can take input in the form of parameters and return output in the form of a return value. In this article, we will discuss function parameters in JavaScript.
Function parameters are the values that are passed to a function when it is called. They are used to provide input to the function. Parameters are defined in the function declaration and are enclosed in parentheses. Multiple parameters can be separated by commas. When the function is called, the values of the parameters are passed in the same order as they are defined in the function declaration.
Here is an example of a function that takes two parameters:
function addNumbers(num1, num2) {
return num1 + num2;
}
In this example, the function addNumbers
takes two parameters, num1
and num2
. When the function is called, the values of the parameters are passed in the same order as they are defined in the function declaration:
let result = addNumbers(5, 10);
console.log(result); // Output: 15
In this example, the values 5 and 10 are passed as arguments to the addNumbers
function. The function adds these two numbers and returns the result, which is stored in the result
variable. The console.log
statement outputs the value of the result
variable, which is 15.
Default parameters are values that are used when no value is passed for a parameter. They are defined in the function declaration using the assignment operator (=). If a value is passed for the parameter, the passed value is used. If no value is passed, the default value is used.
Here is an example of a function that uses default parameters:
function greet(name = "World") {
console.log("Hello, " + name + "!");
}
In this example, the greet
function takes one parameter, name
, which has a default value of "World". If no value is passed for the name
parameter, the default value of "World" is used:
greet(); // Output: Hello, World!
greet("John"); // Output: Hello, John!
In the first example, no value is passed for the name
parameter, so the default value of "World" is used. In the second example, the value "John" is passed for the name
parameter, so that value is used instead of the default value.
Rest parameters are used to pass an arbitrary number of arguments to a function. They are defined in the function declaration using the spread operator (...). The rest parameter must be the last parameter in the function declaration.
Here is an example of a function that uses rest parameters:
function sum(...numbers) {
let total = 0;
for (let number of numbers) {
total += number;
}
return total;
}
In this example, the sum
function takes an arbitrary number of arguments using the rest parameter ...numbers
. The function then loops through each number and adds them together to get the total:
let result = sum(1, 2, 3, 4, 5);
console.log(result); // Output: 15
In this example, the sum
function is called with five arguments. The function adds these numbers together and returns the result, which is stored in the result
variable. The console.log
statement outputs the value of the result
variable, which is 15.
Function parameters are an important part of JavaScript functions. They allow us to pass input to a function and make our code more flexible and reusable. Default parameters and rest parameters are two features that make function parameters even more powerful.