DEV Community

Samantha Ming
Samantha Ming

Posted on ā€¢ Originally published at samanthaming.com

40 4

JavaScript: Skip Values In Destructuring

Alt Text

You can use blanks to skip over unwanted values šŸ¤“

This way you can avoid useless variable assignments for values you donā€™t want during destructuring šŸ‘

You can also prefix "_" followed by the variable name you're disregarding. This helps communicates to your team member that it's a useless variable šŸ¤

// āŒ Ugh, useless variable assignment
const [ignore, keep] = ['ignore', 'keep'];

// āœ… Good (blank space)
const [, keep] = ['ignore', 'keep'];

// āœ… Good ("_" communicates useless variable)
const [_ignore, keep] = ['ignore', 'keep'];
Enter fullscreen mode Exit fullscreen mode

Add Comments to Improve Code Readability

When using the blank space option to skip over values, you can also add comments. This will help communicate to your fellow developers that you are intentionally skipping over the unwanted values.

let [
  chili,
  , // rotten
  , // rancid
  apple,
  olive,
] = ['chili', 'rotten', 'rancid', 'apple', 'olive'];

// OR

let [
  chili,
  /* rotten */,
  /* rancid */,
  keep,
  olive
] = ['chili', 'rotten', 'rancid', 'keep', 'olive'];
Enter fullscreen mode Exit fullscreen mode

Simple Use Case

Here is a simple use case where this could be helpful.

const url = 'www.samanthaming.com/tidbit.jpg';

// 1. Split string by "."
const array = url.split('.'); // [ 'www', 'samanthaming', 'com/tidbit', 'jpg' ]

// 2. Create only the variable we want
const [ , domain, ,type] = array;

// 3. Consuming the variable we created
const name = `${domain}.${type}`;
// 'samanthaming.jpg'
Enter fullscreen mode Exit fullscreen mode

Community Input

@komputarist: The underscore will be quite helpful. At least someone reading the codes won't have to trace what the variables look like. It can be stressful though when there are lots of variables in the destructured item (array or object).

@FPresencia: Learning that you can do [ , valueICareAbout] has been very useful. As most linters complain about unused variables.

@Erik: In TypeScript the convention is just _

@sulco Agreed, but always look at it pragmatically and have code readability in mind. It can get to absurd situation of making your fellow developer (or you in a future) to have to start counting commas to understand the code ;-)

const values = ['ignore', 'ignore', 'keep'];

// āŒ Don't do useless variable assignment
const [a, b, c] = values;

// āœ… if all you need is one value...
const [, keep] = ['ignore', 'keep'];

// šŸ¦„ But also, don't overhack it:
let c = values[2]; // simpler is better
Enter fullscreen mode Exit fullscreen mode

@SamHulick: Parsing comma-deliminated data and grabbing only what you need.

const tooMuchData = '33871,LOC,type1,99.27,FN';
const [, , , price] = tooMuchData.split(',');

console.log(price); // 99.27
Enter fullscreen mode Exit fullscreen mode

@zornwebdev: Or, you can even destructure an array like an object where the key is the index and then you rename the key to the variable name you want const {1: name} = ['ignore', 'keep']. The value for name would be keep due to that being the value at index 1 in the array.

Resources


Thanks for reading ā¤
Say Hello! Instagram | Twitter | Blog | SamanthaMing.com

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (3)

Collapse
 
ntvinhit profile image
Nguyį»…n Trį»ng VÄ©nh ā€¢

And how about useless arguments in a callback when you can't use a blank as a useless variable?
Example:

const measureCallback = (x, y, width, height) => {
// I just need width, height only;
}
this.measure(measureCallback);

Collapse
 
mattbag00 profile image
Matt Bagni ā€¢

I think you can use the rest operator (...args)

Collapse
 
ntvinhit profile image
Nguyį»…n Trį»ng VÄ©nh ā€¢

so:
const measureCallback = (...args) => {
const [ , , width, height] = args;
}

I dont think this is a good idea

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more