Hello! Welcome back to our journey into JavaScript functions.
In our last lesson, we learned how to create functions using both declarations and expressions. We saw that functions are powerful tools for organizing and reusing code. However, the functions we built were static; they performed the same exact task every time.
Today, we're going to unlock the true power of functions by making them dynamic. You will learn how to define parameters for functions and pass arguments during invocation. This allows us to send data into a function, making it a flexible and reusable tool that can operate on different inputs to produce varied results. This is a fundamental concept you'll use constantly when building applications with Next.js and TypeScript.
Parameters vs. Arguments: The Blueprint and the Materials
Before we write any code, it's crucial to understand two key terms: parameters and arguments. Though often used interchangeably, they have distinct meanings.
- Parameters are the variables listed inside the parentheses
()in a function's definition. They act as named placeholders for the data you expect the function to receive. Think of them as the ingredients listed in a recipe. - Arguments are the actual values you pass to the function when you call (or invoke) it. These are the real ingredients you use when you cook.

Making Functions Dynamic with Parameters
Let's see how we can use parameters to make our functions more useful. A function without parameters is like a calculator that can only add 5 + 5. By adding parameters, we build a calculator that can add any two numbers we give it.
To get a feel for the syntax and flow, let's watch a short video that introduces parameters and shows how to use them.
Introduction to Functions in JavaScript – A Beginner’s Guide
This video from Future Fullstack provides a fantastic, beginner-friendly introduction to parameters and arguments. It starts with a simple example and builds on it.
Watch from 04:36 to 08:01. Focus on: How the user parameter is added to the congratulate function definition. The difference between the parameter (user) and the argument ('Daniel'). How the argument's value is used inside the function body.
As the video showed, you define parameters by placing them inside the parentheses of your function declaration, separated by commas if there are more than one.
// A function with one parameter called 'name'
function greet(name) {
console.log(`Hello, ${name}! Welcome.`);
}
// Now we call the function, passing an argument
greet("Alice"); // Output: Hello, Alice! Welcome.
greet("Bob"); // Output: Hello, Bob! Welcome.
We've created one reusable function that can produce different outputs based on the argument we provide.
Using Multiple Parameters
Most useful functions require more than one piece of information. You can define as many parameters as you need, separating them with commas. When you call the function, you must provide the arguments in the same order.

Let's continue with the video to see a practical example of a function with multiple parameters.
Introduction to Functions in JavaScript – A Beginner’s Guide
This next segment from the same Future Fullstack video demonstrates defining and calling a function with multiple parameters.
Watch from 08:01 to 08:40. Notice how the arguments 'Monday' and 'Wednesday' are passed and mapped to the day parameter in the correct order.
Here's another example you can try in your editor: a function to calculate the area of a rectangle.
function calculateRectangleArea(width, height) {
const area = width * height;
console.log(`The area of a rectangle with width ${width} and height ${height} is ${area}.`);
}
calculateRectangleArea(10, 5); // Output: The area of a rectangle with width 10 and height 5 is 50.
calculateRectangleArea(20, 8); // Output: The area of a rectangle with width 20 and height 8 is 160.
Test your understanding!
What will be logged to the console when the following code is executed?
function createCharacter(name, role, level) {
console.log(`${name} the ${role} has reached Level ${level}.`);
}
createCharacter("Arin", 12, "Mage");
Show answer
The output will be: Arin the 12 has reached Level Mage.
This happens because arguments are assigned to parameters based on their order. The first argument "Arin" is assigned to name. The second argument 12 is assigned to role. The third argument "Mage" is assigned to level. The function then uses these values as assigned.
Under the Hood: How Arguments are Passed
You've expressed a strong interest in understanding why things work the way they do. When we pass arguments to functions, JavaScript's behavior depends on the data type of the argument. This is a critical concept known as "pass-by-value" versus "pass-by-reference" (or more accurately, "pass-by-sharing").
To fully grasp this, we need to quickly review how JavaScript stores data in memory. This is explained excellently in the following article.
Understanding JavaScript Variable Passing: Pass by Value ...
This article from Programfarmer, 'Understanding JavaScript Variable Passing', has fantastic diagrams to help visualize memory. It will give you a clear mental model for what happens when you pass data to a function.
Read the first two sections: 'Primitive Types and Objects in Memory' and 'Copying Variables in Memory'. Focus on the visual diagrams and the key difference: Primitives: The value itself is stored and copied. Objects: A reference (memory address) to the value is stored and copied.
Now that you have the mental model of the Stack and Heap, let's apply it to function arguments.
1. Primitive Types are Passed by Value
When you pass a primitive type (like a number, string, or boolean) as an argument, the function receives a copy of that value. The function's parameter is a completely separate variable. Any changes made to the parameter inside the function do not affect the original variable outside of it.
Understanding JavaScript Variable Passing: Pass by Value ...
Let's continue with the same article to see how 'pass by value' works with function parameters.
Read the section 'Understanding Pass by Value Through Function Parameters'. Pay close attention to the diagram showing that a and primitiveData are independent.
Example:
function increaseScore(score) {
score = score + 10;
console.log(`Score inside function: ${score}`); // Score inside function: 20
}
let myScore = 10;
increaseScore(myScore);
console.log(`Score outside function: ${myScore}`); // Score outside function: 10
The original myScore remains unchanged because the function only received a copy of its value.
2. Reference Types are Passed by Sharing
When you pass a reference type (like an object or an array), the function receives a copy of the reference (the memory address). Both the original variable and the function's parameter point to the exact same object in memory.
This means if you modify the contents of the object inside the function, the changes will be visible outside the function.
However, if you reassign the parameter to a completely new object, it only changes what the local parameter points to; the original variable is unaffected. This is why "pass-by-sharing" is a more precise term.
Understanding JavaScript Variable Passing: Pass by Value ...
Finally, let's see how objects behave. This is a very important concept for state management in frameworks like React/Next.js.
Read the sections 'Understanding Pass by Reference...', 'Wait, What About Pass by Sharing?', and the 'Conclusion'. The diagrams are key here. Understand the difference between modifying a property (objectData.number = 10) and reassigning the variable (objectData = { ... }).
Example:
function addPower(player) {
// Modifying the original object's property
player.power = "Flight";
console.log(`Power added inside function.`);
}
let hero = { name: "Alex" };
addPower(hero);
console.log(hero); // Output: { name: 'Alex', power: 'Flight' }
The original hero object was modified because both hero and the player parameter were pointing to the same object in memory.
Handling Mismatched Arguments
What happens if the number of arguments you pass doesn't match the number of parameters defined? JavaScript is very flexible with this.
- Too few arguments: If you don't provide an argument for a parameter, its value inside the function will be
undefined. - Too many arguments: Extra arguments are simply ignored.
The following short text explains this with clear examples.
Learning JavaScript functions and how you should use them
This snippet from Launch School's JavaScript book provides concise examples of what happens with mismatched arguments.
Read Exercise 7 and Exercise 8 (including their solutions). They directly demonstrate what happens when you pass too few or too many arguments.
Practical Exercises
Let's apply these concepts. Open your code editor and try these exercises.
Exercise 1: Simple Greeting Function
- Create a function named
createGreetingthat accepts two parameters:nameandtimeOfDay. - Inside the function, log a message to the console, like "Good
timeOfDay,name!". - Call the function with your name and "morning" as arguments.
- Call it again with a friend's name and "evening" as arguments.
Show solution
// 1. Function definition
function createGreeting(name, timeOfDay) {
// 2. Logic to log the message
console.log(`Good ${timeOfDay}, ${name}!`);
}
// 3. First call
createGreeting("Alex", "morning"); // Output: Good morning, Alex!
// 4. Second call
createGreeting("Brenda", "evening"); // Output: Good evening, Brenda!
Explanation: This exercise demonstrates the basic use of multiple parameters. The arguments "Alex" and "morning" are passed and assigned to the name and timeOfDay parameters respectively, allowing for a dynamic greeting.
Exercise 2: Pass-by-Value with Primitives
- Define a variable
playerHealthand set it to100. - Create a function
takeDamagethat accepts one parameter,health. - Inside the function, subtract
20from thehealthparameter and log the new value. - Call the
takeDamagefunction, passingplayerHealthas the argument. - After the function call, log the value of the original
playerHealthvariable. Is it changed?
Show solution
// 1. Original variable
let playerHealth = 100;
// 2. Function definition
function takeDamage(health) {
// 3. Modify the parameter
health = health - 20;
console.log(`Health inside function: ${health}`); // Output: Health inside function: 80
}
// 4. Call the function
console.log(`Health before function call: ${playerHealth}`); // Output: Health before function call: 100
takeDamage(playerHealth);
// 5. Check the original variable
console.log(`Health after function call: ${playerHealth}`); // Output: Health after function call: 100
Explanation: The original playerHealth variable remains 100. This is a perfect example of pass-by-value. The takeDamage function received only a copy of the number 100. The modification happened to that copy, which is local to the function, leaving the original playerHealth variable untouched.
Exercise 3: Pass-by-Sharing with Objects
- Define a variable
questas an object:{ name: "Find the Lost Sword", completed: false }. - Create a function
completeQuestthat accepts one parameter,questObject. - Inside the function, change the
completedproperty ofquestObjecttotrue. - Call the
completeQuestfunction, passing yourquestobject as the argument. - After the function call, log the entire
questobject. Is it changed?
Show solution
// 1. Original object
let quest = {
name: "Find the Lost Sword",
completed: false,
};
// 2. Function definition
function completeQuest(questObject) {
// 3. Modify the object's property
questObject.completed = true;
}
// 4. Call the function
console.log("Quest before function call:", quest); // Output: Quest before function call: { name: 'Find the Lost Sword', completed: false }
completeQuest(quest);
// 5. Check the original object
console.log("Quest after function call:", quest); // Output: Quest after function call: { name: 'Find the Lost Sword', completed: true }
Explanation: The original quest object's completed property is now true. This demonstrates pass-by-sharing. The completeQuest function received a reference pointing to the same object in memory as the quest variable. By using that reference (questObject), the function was able to directly modify the original object.
Conclusion
Fantastic work today! You've taken a massive step from writing static code to creating dynamic, flexible functions that can handle different inputs. This is the essence of programming: writing abstract logic that can be applied to concrete data.
Key Takeaways:
- Parameters are the named placeholders in a function's definition (
function myFunc(param1) {}). - Arguments are the actual values passed when a function is called (
myFunc(42)). - Arguments are mapped to parameters by order.
- Pass-by-Value (Primitives): The function gets a copy of the value. The original variable is safe from changes.
- Pass-by-Sharing (Objects/Arrays): The function gets a copy of the memory address. It can modify the original object's properties.
Next Up:
Our functions can now accept data, but they can't give anything back to the part of the code that called them, other than logging to the console. In the next lesson, we will learn how to use the return statement to make functions output results that we can store in variables and use in the rest of our program.