🍨Object Literals

In JavaScript, an object literal is a syntax for creating an object with a set of properties and their values. It is a concise way of creating a new object and setting its properties at the same time.

Here is an example of an object literal in JavaScript:

const obj = {
  key1: value1,
  key2: value2,
  key3: value3
};

In this example, obj is an object with three properties: key1, key2, and key3. The values of these properties are value1, value2, and value3, respectively.

You can also use object literals to create nested objects. For example:

const obj = {
  key1: value1,
  key2: {
    nestedKey1: nestedValue1,
    nestedKey2: nestedValue2
  },
  key3: value3
};

In this example, obj has a property key2 that is itself an object with two properties, nestedKey1 and nestedKey2.

Object literals are a powerful and convenient way to create and manipulate objects in JavaScript. They can be used to store data, to define functions, and to create complex data structures.

Last updated