Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

36
Views
what is comma trailing in javascript
const army = {
     name: 'karan',
     memb : [
            {
                captain: 'suraj',
            },
            {
                rifleMan: 'sanjay'
            },
            {
                grenadier: 'yogesh'
            }
      ]
}
    
const { name , memb:[, cap]} = army
    
console.log("cap ", cap)

This gives cap { rifleMan: 'sanjay' }.

Can anyone please explain this concept and what it is called

7 months ago · Juan Pablo Isaza
2 answers
Answer question

0

You are de-structuring the army object. So "karen" is stored in name and from the army.memb which is an array you want the second element and store it in cap.

For example if you put two commas it means you want the third element of the memb array:

const army = {
  name: 'karan',
  memb: [{
      captain: 'suraj',
    },
    {
      rifleMan: 'sanjay'
    },
    {
      grenadier: 'yogesh'
    }
  ]
}

const { name, memb: [,, cap]} = army


console.log("cap ", cap)

7 months ago · Juan Pablo Isaza Report

0

It is feature of array destructuring in javascript. It is used to ignore or skip certain values while destructuring.

const [a, , b] = [1, 2, 3];
console.log(a,b);   // 1,3
const [, a, b] = [1, 2, 3];
console.log(a,b);   // 2,3
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.