does anyone know what this syntax is in Javascript?
I got a function like so:
function myFunc(param) { 
  return {
     [var.prop]: param.func(args);
  };
}
That looks like a colon after an array. Would anyone know what that means in JS? Thanks.
does anyone know what this syntax is in Javascript?
I got a function like so:
function myFunc(param) { 
  return {
     [var.prop]: param.func(args);
  };
}
That looks like a colon after an array. Would anyone know what that means in JS? Thanks.
In normal the object with key we know will be look like,
function myFunc() {
  const newObj = {
    'key': 'value'
  }
  return newObj;
}
console.log(myFunc());In the above you can able to see that the 'key' act as a known string.
Suppose in your app if you get that string in dynamic format then you can add square bracket around like [] and can assign value of that property as a key to an object.
For eg.., You are getting the key from an object like,
const data = {
  prop:'dynamicKey'
};
And you need to assign the value of prop as key to an obj then you can use it like,
[data.prop]: 'value'
const data = {
  prop:'dynamicKey'
};
function myFunc() {
  const newObj = {
    [data.prop]: 'value'
  }
  return newObj;
}
console.log(myFunc());