JavaScript Functions
Lesson Progress0%
Parameters and Arguments
Topic Overview
Take your time to understand each concept. Practice with the examples below!
Functions become really powerful when they can accept and work with different inputs. Let's learn how to make flexible functions!
Parameters vs Arguments:
- •Parameters:Variables listed in the function definition (the placeholders)
- •Arguments:Actual values passed when calling the function
Example:
function greet(name, age) { // 'name' and 'age' are parameters return `Hi ${name}, you are ${age} years old`;}greet("Sam", 25); // "Sam" and 25 are arguments
Default Parameters (backup values):
function greet(name = "Friend") { return `Hello, ${name}!`;}greet(); // "Hello, Friend!" (uses default)greet("Amy"); // "Hello, Amy!" (uses provided value)
Rest Parameters (accept any number of inputs):
function sum(...numbers) { // ...numbers collects all arguments let total = 0; for (let num of numbers) { total += num; } return total;}sum(1, 2, 3); // 6sum(5, 10, 15, 20); // 50
Why this matters:
- •Makes functions flexible and reusable
- •Handles different scenarios gracefully
- •Prevents errors when arguments are missing
Ready to practice? Try the exercise below to reinforce your learning!
Try it yourself
Create a function that accepts any number of arguments and returns their sum.
Loading...