Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax - what does square brackets around a variable declaration mean [duplicate]

Tags:

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

like image 596
Neil Avatar asked Nov 14 '17 13:11

Neil


People also ask

What are square brackets in function syntax represent?

Square brackets are used to index (access) elements in arrays and also Strings.

What does brackets around a variable mean?

A square bracket at one end of an interval indicates that the interval is closed at that end (i.e., the number adjacent to the opening or closing square bracket is included in the interval).

What does square brackets mean in node JS?

Square brackets means new Array.

What do square brackets do in react?

The square bracket is used to get the name of the event target and set the value to the state. For example, if the email field is changed - it fetches the email value and sets it to the state (state object/email value) and does the same to the password as well.


1 Answers

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 assigning to the component variable the first element held in the array that will be returned by router.getMatchedComponents({...to}), where to is an array-like structure turned into object using the spread operation.

like image 82
cнŝdk Avatar answered Oct 14 '22 06:10

cнŝdk