Parameters and Arguments
When you create a function, you use parameters as labeled containers that will eventually hold values. Picture parameters as empty boxes labeled with names, waiting for something to be put inside. When you actually run or "call" the function, you provide argumentsβthese are the real values that fill those boxes.
For example:
- In a function that sends a message, the parameter is the label for the message box;
- The argument is the specific message you want to send, such as
"Hello!".
Parameters are defined in the function's header as part of its structure, while arguments are the data you supply when you use the function. This distinction helps you design functions that are flexible and reusable.
123456function greet(name) { return "Hello, " + name + "!"; } const greeting = greet("Alice"); console.log(greeting); // Output: Hello, Alice!
Default parameters and missing arguments
You can give a parameter a default value in your function definition. This means if you call the function without providing that argument, JavaScript will use the default value you set. Use the following syntax:
function functionName(parameter = defaultValue) {
// ...
}
Benefits of default parameters:
- Make your functions more flexible;
- Prevent errors when arguments are missing;
- Simplify your code by reducing the need for extra checks.
123456function greet(name = "friend") { return "Hello, " + name + "!"; } console.log(greet()); // Output: Hello, friend! console.log(greet("Alice")); // Output: Hello, Alice!
If you do not assign a default value and call the function without an argument, the parameter will be undefined. Using default parameters helps you avoid unexpected undefined values and ensures your functions behave predictably.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 7.69
Parameters and Arguments
Swipe to show menu
When you create a function, you use parameters as labeled containers that will eventually hold values. Picture parameters as empty boxes labeled with names, waiting for something to be put inside. When you actually run or "call" the function, you provide argumentsβthese are the real values that fill those boxes.
For example:
- In a function that sends a message, the parameter is the label for the message box;
- The argument is the specific message you want to send, such as
"Hello!".
Parameters are defined in the function's header as part of its structure, while arguments are the data you supply when you use the function. This distinction helps you design functions that are flexible and reusable.
123456function greet(name) { return "Hello, " + name + "!"; } const greeting = greet("Alice"); console.log(greeting); // Output: Hello, Alice!
Default parameters and missing arguments
You can give a parameter a default value in your function definition. This means if you call the function without providing that argument, JavaScript will use the default value you set. Use the following syntax:
function functionName(parameter = defaultValue) {
// ...
}
Benefits of default parameters:
- Make your functions more flexible;
- Prevent errors when arguments are missing;
- Simplify your code by reducing the need for extra checks.
123456function greet(name = "friend") { return "Hello, " + name + "!"; } console.log(greet()); // Output: Hello, friend! console.log(greet("Alice")); // Output: Hello, Alice!
If you do not assign a default value and call the function without an argument, the parameter will be undefined. Using default parameters helps you avoid unexpected undefined values and ensures your functions behave predictably.
Thanks for your feedback!