How to create an object?

// object literal
const human = {
  name: 'Alice',
  age: 21,
  someProperty: someValue,
  anotherProperty: anotherValue,
  someMethod: function() {
    // code
  },
}
Copy

September 3rd, 2019

# Object literal

Add to bookmarks

A Javascript object literal is a comma-separated list of name-value pairs wrapped in curly braces.

Nice extra features:

  • If the variable name is the same like it's key, then const obj = { name: name } is equal const obj = { name }
  • A shorthand for using methods: someMethod: function() {} becomes someMethod() {}
Tracking with Matomo to improve the UX. See Privacy Policy for more details.