Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the [`expression`] notation used for?

I've recently begun learning reactjs and stumbled upon this piece of code:

handleChange = event => {
  const { name, value } = event.target

  this.setState({
    [name]: value,
  })
}

And I'm somewhat confused, in particular with the following notation:

[name]: value,

What are the [ ] used for?

I understand, that the const = { name, value } = event.target is used for object destruction into properties, but the [name] notation seems strange and I have not seen any previous reference to it in any of my javascript coding.

Despite my best efforts at googling [] javascript usage and similar, I was unable to find anything of similar sort.

My best guess would be the similarity to object["property"] expression, but this still seems a little odd compared to that.

like image 551
Samuel Hulla Avatar asked Feb 16 '26 17:02

Samuel Hulla


1 Answers

These are called computed property names, and the notation is basically for using a variable as an object key.

like image 50
Jayce444 Avatar answered Feb 19 '26 09:02

Jayce444



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!