Created
August 7, 2020 21:00
-
-
Save saqibameen/c1b8fb9db47cef256dec59593d0c417c to your computer and use it in GitHub Desktop.
Use computed keys in objects while creating them.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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