Home Syntax - what does square brackets around a variable declaration mean
Reply: 1

Syntax - what does square brackets around a variable declaration mean

Neil
1#
Neil Published in 2017-11-14 13:55:40Z

This question already has an answer here:

  • Multiple assignment in javascript? What does [a,b,c] = [1, 2, 3]; mean? 4 answers

Take the following line of code

const [component] = router.getMatchedComponents({ ...to })

Could anyone advise what the square brackets around component means here? I have tried to google this but struggling to find an answer

chsdk
2#
chsdk Reply to 2017-11-14 14:04:37Z

It's called Destructuring assignment, and it's used to unpack the values of an array and assign them to new variables.

So here in your code:

const [component] = router.getMatchedComponents({ ...to })

You are declaring a component variable that will hold the object hold in the array that will be returned from router.getMatchedComponents({...to}), where to is an array like structure turned into array using the spread operation.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.312936 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO