Object Literal in JavaScript

An object literal in JavaScript allows us to create plain JavaScript objects. It consists of a list of key-value pairs, each separated by a comma and wrapped inside curly braces. In this tutorial let us learn how to create objects using object Literal with examples. Learn how to use the Object Property Initializer Shorthand syntax etc.

Creating objects using object literal Syntax

The following is an example of creating a new JavaScript object using object literal syntax.

  1. Enclose everything in a curly bracket
  2. Use colon as a separator between a property name (key) and its value
  3. Use a comma as a separator between each key-value pair
  4. Comma at the end of the last key-value pair is ok.

Object Literal Syntax Examples

Spaces and line breaks are not important. You can use multiple lines or combine all of them into a single line

Trailing commas are ok. Note the comma after age:50,

An object with no properties

Object with two numeric properties.

More complex values

These property names can include space. For example 'main title'. You need to use the string inside a quote.

You can also make use of a hyphen

The property names can also include reserved words like for and let. The rules of the identifier do not apply to property names.

The properties can have any value including the objects, functions, etc. The following book object consists of a property author , which is an object.

In the following example, getAuthorName is a function

Property Names

JavaScript allows us to use any string as Property Names. The Strings may include spaces or special characters or even reserved keywords like let. If the name does not confirm the JavaScript identifiers rules, then you need to enclose it in quotes while declaring it.

If you use any other types as property names, then they are converted to a string. For example in the following example, we use numbers as property names, JavaScript converts them to strings, when creating the properties.

object literal vs new Object()

There is no difference between them.

Whenever we create a new object via an object literal, JavaScript invokes the new Object() to create the object. i.e. objects created from object literal inherit properties from Object.prototype.

The above example does the same thing as the following example.

Creating Multiple Objects

Use a function, if you wish to create multiple objects using the same recipe. The following example creates two Person objects.

Object Property Initializer Shorthand

In the above example, property names match the variable name. In such cases, you can use the variable name directly. This syntax was introduced in ES6.

When you use the variable name directly, the JavaScript will look for the similarly named variable in the scope chain. If it finds one, it will create a property with that name and assigns the value of the variable to it.

Another example

You can mix and match both approaches

Property Initializer Shorthand for functions

Similarly, you can define a function without using the function keyword or a colon. Check out the add & get methods in the following example. This option was introduced in ES6.

Getters & Setters

The following example shows how to create Property getters and setters using the object Literal example

Computed Property Name

You can also use an expression, which results in a single value to be used as Property Name. We need to enclose the expression inside square brackets( [])

In the following example, we create a Author Name property in book object

From ES6 onwards, you can use the computed property name as a part of the object literal syntax. For Example, we placed the expression directly inside the square brackets( [])

You can also invoke a function inside square brackets.

The following example uses the fullNameFieldName() function to assign the property name to a function.

Object Literals as Lookups

You can use the objects as lookups. The following code shows how to get values from Object properties.

References

  1. Object Literals

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top