Last active
December 23, 2020 20:41
-
-
Save DoctorDerek/10feca58ed300bd43884da51f9723966 to your computer and use it in GitHub Desktop.
What Is the Object Literal Syntax in JavaScript?
This file contains hidden or 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
| // Typically, you access key with . dot syntax. | |
| const emptyObject = {} | |
| const objectLiteral = { key: "value" } | |
| emptyObject.key = "value" | |
| // These two objects now have the same contents. | |
| // With emojis, you need to use square brackets. | |
| const emptyObject2 = new Object() | |
| const emojiObject = { "β": "π«" } | |
| emptyObject2["β"] = "π«" | |
| // These two objects now have the same contents. | |
| // Values can be variables in object literals. | |
| const key = "π" | |
| const value = "π " | |
| const emptyObject3 = {} | |
| // The literal used the string "key" as the key, | |
| const objectLiteral2 = { key: value } | |
| // but the square brackets use the key variable. | |
| emptyObject3[key] = value | |
| // These objects don't have the same contents: | |
| console.log(objectLiteral2) // {key: "π "} | |
| console.log(emptyObject3) // {π: "π "} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment