0

Can someone explain to me how

const { value, name } = event.target;

is equal to

event.target.name
event.target.value
Bruno Monteiro
  • 4,153
  • 5
  • 29
  • 48
ayanda
  • 31
  • 4
  • 4
    It is called [Destructuring assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment). – Ajeet Shah Mar 09 '21 at 19:39
  • 1
    Also note the second thing you wrote is probably not what you meant, `const value = event.target.value; const name = event.target.name;` is the longhand version. – Jared Smith Mar 09 '21 at 22:59

1 Answers1

0

Destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables. example for arrays :

const x = [1, 2, 3, 4, 5];
const [y, z] = x;
console.log(y); // 1
console.log(z); // 2

another example for destructuring with objects

const user = {
id: 42,
is_verified: true
};

const {id, is_verified} = user;
console.log(id); // 42
console.log(is_verified); // true

reference : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

Moukim hfaiedh
  • 409
  • 6
  • 9