Skip to content

Instantly share code, notes, and snippets.

@saqibameen
Created August 7, 2020 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saqibameen/c1b8fb9db47cef256dec59593d0c417c to your computer and use it in GitHub Desktop.
Save saqibameen/c1b8fb9db47cef256dec59593d0c417c to your computer and use it in GitHub Desktop.
Use computed keys in objects while creating them.
// In ES5.
function createObjWithKey(key) {
let newObj = {};
newObj.[key] = 1;
return newObj;
}
// In ES6
function createObjWithKey(key) {
return { [key]: 1 };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment