Welcome, JavaScript adventurers! Buckle up as we delve into the exciting world of function parameters. These are the essential ingredients you provide to functions, allowing them to perform customized tasks and calculations. This chapter equips you with everything you need to understand, use, and master parameters in JavaScript functions.
function greet(name) { // name is the parameter
console.log("Hello,", name + "!");
}
()
.
greet("Alice"); // Output: Hello, Alice!
greet("Bob"); // Output: Hello, Bob!
greet
function twice, providing different arguments (“Alice” and “Bob”) for the name
parameter. The function uses these arguments to personalize the greeting message.
function calculateArea(width, height) {
const area = width * height;
return area;
}
const result1 = calculateArea(10, 5); // Correct: Two arguments provided (width, height)
const result2 = calculateArea(10); // Error: Missing argument for height
console.log(result1); // Output: 50
// console.log(result2); // This line will cause an error
calculateArea
function expects two parameters, width
and height
. In result1
, we provide both arguments, resulting in the correct area calculation.result2
, we only provide one argument (10
) for width
. This creates a mismatch as the function requires a value for height
as well, leading to an error.
function fullName(firstName, lastName = "Doe") {
const full_name = firstName + " " + lastName;
return full_name;
}
const name1 = fullName("Alice"); // Output: Alice Doe (uses default "Doe")
const name2 = fullName("Bob", "Smith"); // Output: Bob Smith
fullName
function has two parameters: firstName
and lastName
. We assign a default value of “Doe” to lastName
.fullName("Alice")
), we only provide one argument (“Alice”). The function uses the default value “Doe” for the missing lastName
.fullName("Bob", "Smith")
), we provide both arguments, overriding the default value for lastName
....
) allows you to capture an indefinite number of arguments as an array within a function.
function sum(...numbers) {
let total = 0;
for (const num of numbers) {
total += num;
}
return total;
}
const sum1 = sum(1, 2, 3); // Output: 6
const sum2 = sum(5, 10, 15, 20); // Output: 50
sum
function uses the rest parameter ...numbers
. Any number of arguments passed during the call will be collected as an array named numbers
.numbers
array using a for...of
loop, adding each element (argument) to the total
variable.
function greetUser({ name, age }) {
console.log("Hello,", name + "! You are", age, "years old.");
}
const person = { name: "Alice", age: 30 };
greetUser(person); // Output: Hello, Alice! You are 30 years old.
greetUser
function uses destructuring within its parameters. The object passed as an argument is destructured, extracting the name
and age
properties and assigning them to individual variables name
and age
within the function.person
object as an argument. Destructuring unpacks the object’s properties for use within the function.arguments
Object: A Legacy Approach (Use with Caution)arguments
object accessible within functions. It’s an array-like object containing all the arguments passed to the function, regardless of the number of parameters defined.
function printArguments() {
for (let i = 0; i < arguments.length; i++) {
console.log(arguments[i]);
}
}
printArguments(1, "Hello", true); // Output: 1, Hello, true
printArguments
function doesn’t have any defined parameters. It uses the arguments
object to access all arguments passed during the call. We iterate through the arguments
object using a loop to print each argument.arguments
object is a legacy feature with some limitations. It’s generally recommended to use rest parameters (...
) or function destructuring for more modern and flexible argument handling.By understanding and effectively using function parameters, you can create versatile and reusable functions in JavaScript. Remember these key takeaways:Parameters act as placeholders for arguments you provide when calling a function. The number and order of arguments must match the defined parameters. Default parameter values and rest parameters enhance function flexibility. Destructuring allows for clean argument unpacking within functions. While the arguments object exists, consider rest parameters or destructuring for more modern approaches. Happy coding !❤️