Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Parameters and Arguments | Working with Function Data and Scope
Functions in JavaScript

bookParameters 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.

123456
function greet(name) { return "Hello, " + name + "!"; } const greeting = greet("Alice"); console.log(greeting); // Output: Hello, Alice!
copy

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.
123456
function greet(name = "friend") { return "Hello, " + name + "!"; } console.log(greet()); // Output: Hello, friend! console.log(greet("Alice")); // Output: Hello, Alice!
copy

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.

question mark

Which statement correctly describes the difference between parameters and arguments in JavaScript functions?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 7.69

bookParameters 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.

123456
function greet(name) { return "Hello, " + name + "!"; } const greeting = greet("Alice"); console.log(greeting); // Output: Hello, Alice!
copy

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.
123456
function greet(name = "friend") { return "Hello, " + name + "!"; } console.log(greet()); // Output: Hello, friend! console.log(greet("Alice")); // Output: Hello, Alice!
copy

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.

question mark

Which statement correctly describes the difference between parameters and arguments in JavaScript functions?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1
some-alt