Online Compiler logoOnline Compiler

JavaScript Tutorial

JavaScript Parameters and Arguments

Parameters are the placeholders in a function definition, and arguments are the values you pass in.

Understanding the difference makes function calls clearer and safer.

Why We Need It

Most bugs in functions come from unexpected inputs or missing values.

Clear parameter handling keeps your functions predictable.

Syntax

function name(param1, param2) { ... }
name(arg1, arg2)

Basic Example

1. Basic usage

function greet(name) {
  return "Hello " + name;
}

console.log(greet("Neha"));

name is a parameter, and 'Neha' is the argument.

Real World Example

2. Missing argument

function label(status) {
  return status ?? "pending";
}

console.log(label());

Missing arguments become undefined.

Multiple Use Cases

Inputs to Functions

Parameters are the named variables in the function definition.

Arguments are the actual values passed when you call the function.

Default and Optional Values

If an argument is missing, its parameter becomes undefined.

Use default parameters to handle missing values safely.

Argument Flexibility

JavaScript does not enforce parameter count. Extra arguments are ignored unless you capture them.

Use rest parameters to collect extra values.

More Examples

3. Extra arguments

function add(a, b) {
  return a + b;
}

console.log(add(2, 3, 4));

Extra arguments are ignored unless you collect them.

4. Rest parameters

function sum(...nums) {
  return nums.reduce((acc, n) => acc + n, 0);
}

console.log(sum(1, 2, 3));

Rest parameters collect extra arguments into an array.

Comparison

Without

function greet(name) {
  return "Hello " + name;
}
console.log(greet());

With

function greet(name = "Guest") {
  return "Hello " + name;
}
console.log(greet());

Common Mistakes and Fixes

Assuming required arguments

Validate inputs or provide defaults.

Using arguments object in arrows

Arrow functions do not have their own arguments object.

Mixing parameter order

Keep parameter order stable and well-documented.

Interview Questions

What is a parameter?

A named input in a function definition.

What is an argument?

The actual value passed to a function call.

How do you handle extra arguments?

Use rest parameters to capture them.

Practice Problem

Practice: Write a function that takes two parameters and returns their difference.

// TODO: function diff(a, b)

One Possible Solution

function diff(a, b) {
  return a - b;
}

console.log(diff(10, 4));

Frequently Asked Questions

What is the difference between parameters and arguments?

Parameters are named inputs; arguments are the values passed.

What happens if I pass fewer arguments?

Missing parameters become undefined.

Can I pass more arguments?

Yes, extra arguments are ignored unless you capture them.

Try It Yourself

Try passing different arguments and see how outputs change.