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.The number and order of arguments you provide during a function call must strictly match the defined parameters. Any mismatch can lead to errors or unexpected behavior
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
variableFunction arguments are similar to local variables, but they receive their values from the arguments provided during the function call. These arguments are matched with the defined parameters in order
function greet(name) {
console.log("Hello,", name + "!");
}
greet("Bob"); // Argument "Bob" is assigned to the local variable name within greet
greet
function has a parameter `name
. When you call greet("Bob")
, the argument “Bob” is passed and assigned to the local variable name
within the function’s scope.
sayHi(); // This works even though sayHi is defined below
function sayHi() {
console.log("Hello from the hoisted function!");
}
const greet = function(name) {
console.log("Hi,", name);
};
greet("Alice"); // Output: Hi, Alice
const person = {
firstName: "Alice",
lastName: "Smith",
greet: function() {
console.log("Hello,", this.firstName + " " + this.lastName);
},
};
person.greet(); // Output: Hello, Alice Smith (this refers to the person object)
Explanation:
person
with properties and a method greet
.person.greet()
, we invoke the greet
method as if it were a property of the person
object. The this
keyword inside the method refers to the person
object, allowing access to its properties.
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
sayHiLater
function takes a name and a delay. It uses setTimeout
to schedule the execution of a callback function after the specified delay.setTimeout
) is responsible for logging the greeting message.Function invocation is the cornerstone of bringing your JavaScript code to life. By understanding the concepts like arguments, return values, scope, and advanced techniques, you can effectively execute functions and achieve desired outcomes in your programs Happy coding !❤️