The Ultimate Guide to JavaScript Function Parameters
Anik Sikder

Anik Sikder @anik_sikder_313

About: Full-Stack Dev & Tech Writer | Python (Django, FastAPI), JS (React, Next.js) | I build fast, scalable apps & write on backend, frontend, APIs & Web3 | Open to freelance/remote roles

Location:
Dhaka, Bangladesh
Joined:
Aug 6, 2025

The Ultimate Guide to JavaScript Function Parameters

Publish Date: Aug 20
1 0

Functions are the beating heart of JavaScript. They let us organize code, reuse logic, and build anything from small utilities to giant frameworks. But what really makes functions flexible are their parameters. These little guys decide how our functions receive input and how dynamic they can become.

In this deep dive, we’ll explore everything about parameters and arguments in JavaScript. Buckle up, because by the end, you’ll be handling function parameters like a pro magician pulling endless tricks from a hat!


Arguments vs Parameters

Let’s clear the confusion first.

  • Parameters are the names listed in the function definition.
  • Arguments are the actual values you pass when you call the function.
function greet(name) { // 'name' is a parameter
  console.log("Hello, " + name);
}

greet("Alice"); // "Alice" is an argument
Enter fullscreen mode Exit fullscreen mode

Think of it like a restaurant: the menu shows parameter names, and the dish you order is the argument.


Positional Arguments

JavaScript functions normally take arguments in order.

function divide(a, b) {
  return a / b;
}

divide(10, 2); // 5
divide(2, 10); // 0.2
Enter fullscreen mode Exit fullscreen mode

Here, a gets the first argument, b gets the second. Mix them up, and the result changes drastically.

Unlike Python, JavaScript doesn’t have “keyword arguments” by default. But don’t worry-we’ll explore how to simulate them.


Simulating Keyword Arguments with Objects

Keyword arguments let you specify values by name, not position. While JS doesn’t support them natively, we can use objects.

function createUser({ name, age, isAdmin = false }) {
  return { name, age, isAdmin };
}

const user = createUser({ age: 25, name: "Anik" });
console.log(user);
Enter fullscreen mode Exit fullscreen mode

Notice how we didn’t care about order. This is keyword-like behavior.


Unpacking Iterables (a.k.a Spread Operator)

The spread operator (...) is one of the coolest tricks in JS.

function sum(a, b, c) {
  return a + b + c;
}

const numbers = [1, 2, 3];
console.log(sum(...numbers)); // 6
Enter fullscreen mode Exit fullscreen mode

Here, ...numbers unpacks the array elements into separate arguments.


Extended Unpacking

You can unpack part of an iterable and still grab the rest with rest parameters.

function logFirstAndRest(first, ...rest) {
  console.log("First:", first);
  console.log("Rest:", rest);
}

logFirstAndRest(1, 2, 3, 4, 5);
// First: 1
// Rest: [2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

This is like saying, “Give me the first item, and then give me the box of everything else.”


*args in JavaScript (Rest Parameters)

In Python, you might’ve seen *args. JavaScript has something similar with ....

function addAll(...nums) {
  return nums.reduce((a, b) => a + b, 0);
}

console.log(addAll(1, 2, 3, 4, 5)); // 15
Enter fullscreen mode Exit fullscreen mode

No matter how many arguments you pass, ...nums captures them all.


Keyword Arguments with **kwargs (JS Style)

JS doesn’t have **kwargs, but we can mimic it using objects again.

function configure(options) {
  const defaults = { darkMode: false, version: "1.0" };
  return { ...defaults, ...options };
}

console.log(configure({ darkMode: true }));
Enter fullscreen mode Exit fullscreen mode

This lets us pass any number of “named” arguments without worrying about order.


Putting It All Together

Imagine building a function timer:

function timeFunction(fn, ...args) {
  const start = performance.now();
  const result = fn(...args);
  const end = performance.now();
  console.log(`Time taken: ${end - start}ms`);
  return result;
}

function multiply(a, b) { return a * b; }
timeFunction(multiply, 20, 30);
Enter fullscreen mode Exit fullscreen mode

We passed a function, its arguments, and timed the execution. Elegant, right?


Default Parameters (with Caution!)

You can assign default values to parameters:

function greet(name = "stranger") {
  console.log(`Hello, ${name}`);
}

greet(); // Hello, stranger
Enter fullscreen mode Exit fullscreen mode

But beware: defaults are evaluated at call time, not definition time.

function append(item, list = []) {
  list.push(item);
  return list;
}

console.log(append(1)); // [1]
console.log(append(2)); // [2], not [1,2] (new array every time)
Enter fullscreen mode Exit fullscreen mode

This is good, but if you use objects as defaults, be careful, they can persist state across calls if reused.


Final Thoughts

JavaScript function parameters are like a Swiss Army knife-simple at first glance, but full of hidden tools. We covered:

  • Parameters vs Arguments
  • Positional vs Keyword (via objects)
  • Spread and Rest (... magic)
  • Default values (and their pitfalls)
  • Combining everything for flexible, clean code

Once you master these, writing functions becomes less about syntax and more about expressing ideas clearly. Functions stop being rigid blocks and start feeling like Lego you snap them together however you like.


✨ Now go experiment! Try mixing rest parameters with defaults, spread arrays into arguments, and simulate keyword arguments with objects. You’ll be amazed how much cleaner your JavaScript becomes.

Comments 0 total

    Add comment