Sitemap
Sirisha komma

Sirisha komma

Array and object destructuring,Rest and Spread operators

1 min readMay 11, 2025

// Array of arrays containing product names and prices
const products = [
[“Laptop”, 1000],
[“Phone”, 500],
[“Tablet”, 700]
];

// Destructure the second product
const [, [secondProductName, secondProductPrice]] = products;

// Print output
console.log(`Second product: ${secondProductName}`);
console.log(`Second product price: ${secondProductPrice}`);
output:
Second product: Phone
Second product price: 500

object destructuring

// Object representing a review
const review = {
title: “Amazing Product”,
description: “This product exceeded my expectations.”,
author: “John Doe”,
timePosted: “2024–11–24”
};

// Destructure properties from the review object
const { author, title, timePosted } = review;

// Print output
console.log(`${author} posted a review titled, ${title} at ${timePosted}.`);

output:
John Doe posted a review titled, Amazing Product at 2024–11–24.

Get Sirisha komma’s stories in your inbox

Join Medium for free to get updates from this writer.

let {PI} = Math; is same as let PI=Math.PI;
PI===Math.PI // true
PI=1;
PI===Math.PI //false

Spread:
The spread operator allows you to pass all array elements into a function without having to type them all individually.
that is spread operator is used to unpack a box

Rest:
Rest operator on the other hand used to build a small box;
const top7=[“New jersey”,”Newyork”,”california”,”Atlanta”,”West virginia”,”pensilvania”,”kentuky”,”ohio”];
const [first,second,third,…secondVisit]=top7

The rest operator allows you to take items from an array and use them to create a separate sub-array.

const meal = [“soup”, “steak”, “ice cream”]
let [starter] = meal;
console.log(starter);
output: soup

--

--