0

I've found a JavaScript arrow function that looks something like this:

([a,b,c]) => {
  a = 1;
  b = 2;
  c = 'x';
}

How is this function invoked? Also, what is this construct called?

1

2 Answers 2

3

This is an arrow function, which gets an array as a parameter and destruct the first 3 values into the corresponding parameters - a,b,c. But it must be assigned to a variable or be self invoked.

() => {} - Arrow function

[a,b,c] - Array destructuring

Example

const func = ([a,b,c]) => {
  console.log(a);
  console.log(b);
  console.log(c);
};

func([1,2,3,4]);

Sign up to request clarification or add additional context in comments.

Comments

0
([a, b, c]) => {}

The first part is a destructuring assignment in the parameters, which takes an array as parameter and returns the variables with the values of the position.

The later assignment makes no sense with the given code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.