Course contentsShow
← JavaScript
Lesson 415 of 2,72311. FunctionsFree lesson

Parameter Destructuring Basics

Destructure object or array arguments directly in the parameter list for cleaner code.

Parameter Destructuring Basics

You'll learn: How to extract values from objects or arrays directly in a function's parameter list, making your code cleaner and more expressive.


What Is Parameter Destructuring?

Instead of passing an entire object or array to a function and then unpacking it inside the function body, you can destructure it right in the parameter list. This means you're pulling out the specific properties or elements you need at the moment the function receives its arguments.

Think of it like ordering food for delivery: instead of receiving a big sealed box and then opening it to find your burger and fries, the delivery person hands you the burger in one hand and the fries in the other—already separated and ready to use.

Object Destructuring in Parameters

// Without destructuring
function greet(user) {
  console.log(`Hello, ${user.name}! You are ${user.age} years old.`);
}

// With destructuring
function greet({ name, age }) {
  console.log(`Hello, ${name}! You are ${age} years old.`);
}

greet({ name: 'Alice', age: 30 }); // "Hello, Alice! You are 30 years old."

The function automatically extracts name and age from the object argument.

Array Destructuring in Parameters

function showCoordinates([x, y]) {
  console.log(`X: ${x}, Y: ${y}`);
}

showCoordinates([10, 20]); // "X: 10, Y: 20"

You can even combine parameter destructuring with default values:

function createProfile({ name, role = 'Guest' }) {
  console.log(`${name} is a ${role}`);
}

createProfile({ name: 'Bob' }); // "Bob is a Guest"

Key Takeaway: Parameter destructuring lets you unpack object properties or array elements directly in the parameter list, making your function signatures more readable and eliminating repetitive extraction code inside the function body.