Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära 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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you explain the difference between parameters and arguments with another example?

How do default parameters work with multiple parameters?

What happens if I pass undefined as an argument when a default parameter is set?

Awesome!

Completion rate improved to 7.69

bookParameters and Arguments

Svep för att visa menyn

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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 1
some-alt