Course

Parameters vs Arguments

Loading...

In this lesson, you'll learn about the difference between and in JavaScript functions, which is crucial for understanding how data is passed to functions.

Parameters vs Arguments

If you’re new to JavaScript, you may have heard the terms and used interchangeably. While very similar, there is an important distinction between these two concepts.

  • are used when defining a function. They are the names created in the function definition. A parameter acts like a variable that is only meaningful inside the function. It won't be accessible outside of the function.
  • are the real values passed to the function when making a function call .

Let's revisit our example to illustrate this distinction. We would say that our function accepts one . Parameters can be named anything. The only thing that matters is the order. Let's try replacing with .

const sayHi = (firstName) => {
  console.log(`Hi, ${firstName}`);
}

sayHi('Joe'); // Hi, Joe

Nothing changed; our function still works. This shows that are just names we create for the we're planning to pass into the function . As you can see, when calling the function, we have one . The argument is a real JavaScript value, in this case, a of .

Let's try adding another to practice a bit more. Suppose we want to take in both the name and the age of the person. To do that, we'd need to add a second , separated by a comma . We also need to provide an to fill the value of age when making a function call:

const logAge = (name, age) => {
  console.log(`${name} is ${age} years old.`);
}

logAge('Joe', 25); // Joe is 25 years old.

Understanding the difference between and is key to effectively using functions in JavaScript. As you practice, you'll become more comfortable with how data flows through your functions .

Loading...

0 Comments

"Please login to view comments"

glass-bbok

Join the Conversation!

Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.

Upgrade your account
tick-guideNext Lesson

Best Practices for Naming Functions in JavaScript