Table of contents
1.
Introduction
2.
Description
2.1.
Key Features of Object Literals
2.2.
Why Use Object Literals?  
3.
Syntax
4.
Examples of Object Literals in JavaScript
4.1.
Example 1: Creating a Simple Object
4.2.
Example 2: Object with Methods
4.3.
Example 3: Nested Objects
4.4.
Example 4: Adding and Removing Properties Dynamically
5.
Browser Compatibility
6.
Why Should You Use Object Literals?
7.
Object Literal vs. Object Constructor
8.
Frequently Asked Questions
8.1.
What is an object literal in JavaScript?
8.2.
Can object literals have functions?
8.3.
How do you delete a property from an object literal?
9.
Conclusion
Last Updated: Feb 8, 2025
Easy

Object Literals in JavaScript

Author Rahul Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Object literals in JavaScript provide a simple way to create objects using key-value pairs. They are widely used for storing data, configurations, and method definitions in a structured format. This approach enhances code readability and efficiency. Object literals support features like shorthand syntax and computed property names.

Object Literals in JavaScript

In this article, we will learn about object literals in JavaScript, their features, how they work, and how to use them to create and manage objects efficiently.

Description

Object Literals in JavaScript are a simple & efficient way to create objects. Unlike other methods, such as using constructors or classes, object literals allow you to define an object directly using a pair of curly braces `{}`. Inside these braces, you can add properties & methods as key-value pairs. This makes object literals a go-to choice for organizing data in JavaScript.  

Key Features of Object Literals

  • Easy to define and use.
     
  • Can store multiple data types.
     
  • Can include functions (methods) inside objects.
     
  • Do not require class-based instantiation.

Why Use Object Literals?  

  • Simplicity: Object literals are easy to write & understand.  
     
  • Flexibility: You can add or remove properties dynamically.  
     
  • Readability: They make your code clean & organized.  

Syntax

An object literal is defined using curly braces {} and consists of key-value pairs. The syntax is as follows:

let objectName = {
    key1: value1,
    key2: value2,
    key3: value3
};

 

  • Keys: These are property names (strings) that represent different attributes of the object.
     
  • Values: These can be strings, numbers, arrays, functions, or even other objects.

Examples of Object Literals in JavaScript

Example 1: Creating a Simple Object

let student = {
    name: "John",
    age: 21,
    course: "Computer Science"
};
console.log(student.name); 
console.log(student.age);  
You can also try this code with Online Javascript Compiler
Run Code


Output: 

John
21

Example 2: Object with Methods

let car = {
    brand: "Toyota",
    model: "Corolla",
    year: 2022,
    displayInfo: function() {
        return this.brand + " " + this.model + " (" + this.year + ")";
    }
};
console.log(car.displayInfo());
You can also try this code with Online Javascript Compiler
Run Code


Output: 

Toyota Corolla (2022)

Example 3: Nested Objects

let person = {
    name: "Alice",
    address: {
        city: "New York",
        zip: "10001"
    }
};
console.log(person.address.city); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

New York

Example 4: Adding and Removing Properties Dynamically

In JavaScript, you can use variables as property names in object literals. This is useful when you don’t know the property names in advance.  

// Using dynamic property names
let propertyName = "username";
let user = {
    [propertyName]: "john_doe",
    age: 25,
    isAdmin: false
};


console.log(user.username); 
You can also try this code with Online Javascript Compiler
Run Code


In this example:  

  • The `propertyName` variable is used as the key for the `username` property.  
     
  • This allows you to create flexible & dynamic objects.  


Output: 

john_doe

Browser Compatibility

Object Literals in JavaScript are a fundamental part of JavaScript and work in all modern browsers, including:

  • Google Chrome
     
  • Mozilla Firefox
     
  • Microsoft Edge
     
  • Safari
     
  • Opera
     
  • Internet Explorer (IE 6+)


Since object literals are part of standard JavaScript (ECMAScript), they are fully supported across different environments.

Why Should You Use Object Literals?

Object Literals in JavaScript are widely used because they offer:

  1. Simplicity: No need for class-based object creation.
     
  2. Readability: Code is easier to understand and maintain.
     
  3. Performance: Object literals are faster to create than constructor-based objects.
     
  4. Flexibility: Can store various data types and be modified dynamically.
     
  5. Better Data Management: Helps organize related data together.

Object Literal vs. Object Constructor

ParametersObject LiteralObject Constructor
Syntax ComplexitySimpleMore complex
PerformanceFasterSlower
ReadabilityMore readableLess readable
FunctionalityBasic use casesAdvanced use cases

Frequently Asked Questions

What is an object literal in JavaScript?

An object literal is a way to define an object in JavaScript using curly braces {} with key-value pairs, making object creation simple and efficient.

Can object literals have functions?

Yes, object literals can have functions (methods) inside them. These functions can access object properties using the this keyword.

How do you delete a property from an object literal?

You can use the delete keyword to remove a property from an object literal

Conclusion

In this article, we learned about Object Literals in JavaScript and how they help create objects easily using key-value pairs. We discussed their simple syntax, benefits, and how they make code more readable and easy to manage. Understanding object literals allows developers to write clean and structured JavaScript code, improving the efficiency of web applications.

Live masterclass