Piotr Sikora - Logo - Automatyzacja procesów | AI | JavaScript | Front End | Team Leader
  • Home
  • Services
    • Process Automation
    • AI for Lawyers
  • Blog
  • Quotes
  • Contact
PL/EN

Master JavaScript Destructuring for Cleaner Code

  • Home
  • Blog
  • Master JavaScript Destructuring for Cleaner Code
Master JavaScript Destructuring for Cleaner Code

By Piotr Sikora

  • javascript

  • 4 min read

Table of Contents

  • Using Destructuring assignment to assign values to variables/consts
  • Destructuring assignment nested elements in object
  • Destructuring assignment and undefined elements in object
  • Destructuring assignment and undefined nested elements in object

Let’s imagine that we have an object which we want to access. This object will look like this:

let req = {
  params: {
    id: 'Request params ID',
    params: {
        id: 'Internal ID'
    }
  },
  body: 'Request body'
};

To access elements of this object we can:

req.params;
req.body;

To display them in console:

console.log(req.params);
console.log(req.body);

In case we want to assign those properties into variables or consts:

const params = req.params;
const body = req.body;

So in case this object had more properties like:

let req = {
  params: {
    id: 'Request params ID',
    params: {
        id: 'Internal ID'
    }
  },
  body: 'Request body',
  param2: 'value of param2',
  param3: 'value of param3',
  param4: 'value of param4',
};

and we would like to assign them to variables/consts we would need to have:

const params = req.params;
const body = req.body;
const param2 = req.param2;
const param3 = req.param3;
const param4 = req.params4;

Using Destructuring assignment to assign values to variables/consts

In this case we can use Destructuring assignment:

const { params, body, param2, param3, param4 } = req;

Now object req is estructuring and const params will be equal req.params

To check how it works let’s run this code:

let req = {
  params: {
    id: 'Request params ID',
    params: {
        id: 'Internal ID'
    }
  },
  body: 'Request body',
  param2: 'value of param2',
  param3: 'value of param3',
  param4: 'value of param4',
};

const { params, body, param2, param3, param4 } = req;

console.log('params: ', params); // { id: 'Request params ID', params: { id: 'Internal ID' } }
console.log('body: ', body); // 'Request body'
console.log('param2: ', param2); // 'value of param2'
console.log('param3: ', param3); // 'value of param3'
console.log('param4: ', param4); // 'value of param4'

Destructuring assignment nested elements in object

Lets imagine that we need to access nested element. In this case we will try to access id value which is nested in params. Normally we would need to make it using req.body.id. Lets try to make it with Destructuring assignment

let req = {
  params: {
    id: 'Request params ID',
    params: {
        id: 'Internal ID'
    }
  },
  body: 'Request body',
};

const { params: {id} } = req;

console.log('id: ', id); // 'Request params ID'

This will return ‘Request params ID’

Destructuring assignment and undefined elements in object

Lets imagine that we are now using Destructuring assignment but our object is not “complete”.

let req = {
  params: {
    id: 'Request params ID',
    params: {
        id: 'Internal ID'
    }
  },
  body: 'Request body',
  // param2: 'value of param2', // param2 doesn't exist
  param3: 'value of param3',
  param4: 'value of param4',
};

const { params, body, param2, param3, param4 } = req;

console.log('param2: ', param2); // undefined

If element doesn’t exist it will return undefined value;

Destructuring assignment and undefined nested elements in object

Lets imagine that we are now using Destructuring assignment for object:

let req = {
  // params: {
  //   id: 'Request params ID',
  //   params: {
  //       id: 'Internal ID'
  //   }
  // },
  body: 'Request body',
  // param2: 'value of param2', // param2 doesn't exist
  param3: 'value of param3',
  param4: 'value of param4',
};

const { params: {id}, body, param2, param3, param4 } = req;

console.log('id: ', id); // undefined

If element doesn’t exist it will return error: Uncaught TypeError: Cannot destructure property `id` of ‘undefined’ or ‘null’.;

Share this article

TwitterLinkedInFacebook

Tags:

  • #front-end

  • #javascript

  • #js

  • #typescript

Categories

after-hours(1)AI(7)ai-en(1)angular(4)automatic-tests(1)Automation(2)cryptography(1)css(8)CyberSecurity(2)Development(6)DevOps(1)events(3)javascript(11)n8n(10)ollama(1)security(2)seo(1)

Recent Posts

Testing Kimi Code: First Impressions from Web and CLI

Development

Testing Kimi Code: First Impressions from Web and CLI

Why You Shouldn't Cram Multiple Webhooks Into One n8n Workflow

Automation

Why You Shouldn't Cram Multiple Webhooks Into One n8n Workflow

DRY, WET, AHA: Finding the Right Balance in Code Reuse

Development

DRY, WET, AHA: Finding the Right Balance in Code Reuse

API vs Webhook: Understanding the Difference

Development

API vs Webhook: Understanding the Difference

RTCROS Framework: Structure Your Prompts for Better AI Results

AI

RTCROS Framework: Structure Your Prompts for Better AI Results

About Me

Piotr Sikora - Process Automation | AI | n8n | Python | JavaScript

Piotr Sikora

Process Automation Specialist

I implement automation that saves time and money, streamlines operations, and increases the predictability of results. Specializing in process automation, AI implementation, and workflow optimization using n8n, Python, and JavaScript.

n8n Workflows

n8n workflow automation templates

Explore my workflow templates on n8n. Ready-to-use automations for blog management, data collection, and AI-powered content processing.

3Workflow Templates
View Templates

• Auto-Categorize Blog Posts with AI

• Collect LinkedIn Profiles

• Export WordPress Posts for SEO

Tags

activepiecesafter-hoursahrefsaiAI cost reductionai-agentsai-automationangularantigravityapiAqua Securityarcade-gamesarchitectureautomationbackendbest-practicescadillacs-and-dinosaurschatbotchatgptCI/CD
“If you get on the wrong train, get off at the nearest station”
Japanese proverb
View more quotes

Similar Articles

Discover more related content

Vue, Vue Router, Vuex with TypeScript - Part 1

Vue, Vue Router, Vuex with TypeScript - Part 1

This article will be an introduction how to create project based on Typescript using Vue and Vuex.

Conditional (Ternary) Operator in JavaScript Guide

Conditional (Ternary) Operator in JavaScript Guide

Let's imagine that we could optimize / minify if statement. What could we do?

Unleash the Power of Autoexecuted Objects in JavaScript

Unleash the Power of Autoexecuted Objects in JavaScript

Sometimes you will need to create autoexecuted object (for example in case of load/document ready events)

31 January 2014
Piotr Sikora | ai | n8n | javascript | python

Let's get to know each other!

Get in touch with me
Piotr Sikora | Process Automation  | AI Implementation 🤖 | Technology Consulting
Quick links
  • Home
  • Blog
  • Contact
Contact
  • piotr.sikora.ck@gmail.com
  • +48 505 684 661

© Piotr Sikora 2026 | All Rights Reserved