How to use computed property name in javascript?

How to use computed property name in javascript?

What does mean by Computed Property?

A computed property is a feature that allows expressions to be put in brackets. that is computed and utilised as a property name.

let's assume you needed to make a function that took in two contentions (key, value) and returned an object utilizing those contentions.

Prior to Computed Property Names, Because the property name on the object was a variable (key), you'd need to make the object first, then, at that point use bracket notation to assign that property to the value.

function keyObject (key, value) {
let obj = {}
 obj[key] = value
  return obj
}
keyObject('language', 'Javascript')

you can utilize object literal notation to allocate the expression as a property on the object without creating it first. So the code above would now be able to be reworked this way.

function keyObject (key, value) {
  return {
    [key]: value
  }
}

keyObject('language', 'Javascript')

Where key can be any expressions long as it's enclosed by brackets, [].

Thank you for reading