Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save DoctorDerek/10feca58ed300bd43884da51f9723966 to your computer and use it in GitHub Desktop.

Select an option

Save DoctorDerek/10feca58ed300bd43884da51f9723966 to your computer and use it in GitHub Desktop.
What Is the Object Literal Syntax in JavaScript?
// 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