惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
B
Blog
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学
爱范儿
爱范儿
博客园_首页
博客园 - 聂微东
量子位
V
Visual Studio Blog
aimingoo的专栏
aimingoo的专栏
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
Netflix TechBlog - Medium
F
Fortinet All Blogs
The Cloudflare Blog
T
Tailwind CSS Blog
G
Google Developers Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
腾讯CDC

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Mastering Destructuring in JavaScript: Extract Smarter, C...
Ritam Saha · 2026-04-28 · via DEV Community

Introduction

Imagine checking through a cluttered backpack to grab just your keys, wallet, and phone; tedious, right? That's how grabbing data from arrays or objects feels without destructuring.
Destructuring is JavaScript's elegant shortcut for unpacking values into variables. Since ES6, it revolutionized how we handle data, slashing boilerplate and boosting readability. In this post, we'll break it down step-by-step, with real-world examples showing before-and-after magic.


What Destructuring Means

Destructuring lets you extract values from arrays or objects directly into distinct variables in one line. It's like assigning multiple variables at once, pulling specific pieces without the usual dot-notation, square-braces-notation husstle or index-based access.


Destructuring Arrays: Position-Based Extraction

Arrays destructure by position. Match variables to array indices from left to right.

Before (repetitive):

const scores = [85, 92, 78];
const first = scores[0];
const second = scores[1];
const third = scores[2];

Enter fullscreen mode Exit fullscreen mode

After (clean):

const [first, second, third] = [85, 92, 78];
console.log(first); // 85

Enter fullscreen mode Exit fullscreen mode

Skip elements with commas, or grab the rest with ...:

const [first, , third, ...rest] = [85, 92, 78, 100];
console.log(first, third, rest); // 85, 78, [100]

Enter fullscreen mode Exit fullscreen mode

Array Destructuring

Use case: Processing function arguments or API response of array-type, maybe like extracting lat/long from coordinates.


Destructuring Objects: Key-Based Extraction

Objects use curly braces {} and match by property names. Order doesn't matter—it's key-driven.

Before (verbose):

const user = { name: 'Ritam', age: 20, city: 'Kolkata' };
const name = user.name;
const age = user.age;
const city = user.city;

Enter fullscreen mode Exit fullscreen mode

After (elegant):

const user = { name: 'Ritam', age: 20, city: 'Kolkata' };
const { name, age, city } = user;

console.log(name) // Ritam
console.log(age) // 20

Enter fullscreen mode Exit fullscreen mode

Object Destructuring

You can also rename on the fly: { oldName: newName }.

Use case: Handling JSON response from APIs after parsing, like pulling id and title from fetched posts.


Default Values: Handling Optional/Missing Data

What if a property is not defined or passed? Defaults steps in seamlessly.

const config = { theme: 'dark' };
const { theme = 'light', fontSize = 16 } = config;
console.log(theme, fontSize); // 'dark', 16

Enter fullscreen mode Exit fullscreen mode

Use case: API responses with optional fields, preventing errors in your Node.js backend. Some times while we make standardized API-Response or API-Error handler, this feels gold-mine!!


Benefits of Destructuring and Use Cases

Destructuring shines by cutting repetition - swap 5+ lines for 1. It's helpful for functions, nested-friendly, and readable.

Before vs. After in a Function:

// Before
function greet(user) {
  console.log(`Hi, ${user.name}! You're ${user.age} from ${user.city}.`);
}
greet({ name: 'Ritam', age: 20, city: 'Kolkata' });

// After
function greet({ name, age, city }) {
  console.log(`Hi, ${name}! You're ${age} from ${city}.`);
}

Enter fullscreen mode Exit fullscreen mode

Benefits List:

  • Reduces boilerplate: No more user.name everywhere.
  • Improves readability: Variables mirror data structure.
  • Safer with defaults: Graceful fallbacks.
  • Use cases: React props (const { title, body } = post), Node.js req.body parsing, or nested API data like const { user: { email } } = response.

Wrap Up: Level Up Your Code Standard

Destructuring isn't just syntax sugar—it's a mindset shift toward cleaner, more maintainable code. Next time you're wrestling with objects in your Java/Node projects, reach for it. Practice on a small GitHub repo, and watch your pull requests glow. What's your favorite destructuring trick? Drop it in the comments!