Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Object Literals in JavaScript


In JavaScript, objects are essential data structures used to store collections of data and more complex entities. One of the simplest and most common ways to create objects in JavaScript is by using object literals. An object literal is a concise way of defining an object by directly specifying its properties and values within curly braces {}.

What is an Object Literal?

An object literal allows you to define an object by specifying its properties and corresponding values in a simple, readable format. Each property is defined as a key-value pair, with the key being a string and the value being any valid JavaScript data type.

Syntax of Object Literals:

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

Here, key1, key2, and key3 represent the property names, and value1, value2, and value3 represent the values of those properties.

Creating an Object Literal

Let's create an object that represents a person using an object literal:

let person = {
  name: "Alice",
  age: 30,
  job: "Developer"
};

console.log(person);
    

In this example, the object person has three properties: name, age, and job, each with corresponding values. To access these properties, you can use dot notation or bracket notation:

console.log(person.name); // Output: Alice
console.log(person["age"]); // Output: 30
    

Using Methods in Object Literals

Objects can also have methods (functions) as their values. You can define methods inside an object literal using function syntax.

let person = {
  name: "Alice",
  greet: function() {
    console.log("Hello, " + this.name);
  }
};

person.greet(); // Output: Hello, Alice
    

In this example, the object person contains a method greet, which logs a greeting message using the name property. The this keyword is used to reference the object's properties.

Method Shorthand Syntax

JavaScript provides shorthand syntax for defining methods inside objects. Instead of writing key: function() { }, you can simply write key() { }.

let person = {
  name: "Alice",
  greet() {
    console.log("Hello, " + this.name);
  }
};

person.greet(); // Output: Hello, Alice
    

This is a cleaner way to define methods inside object literals.

Computed Property Names

JavaScript allows you to use expressions as property names in an object literal. This is called computed property names. You can use square brackets to define property names dynamically.

let dynamicKey = "job";
let person = {
  name: "Alice",
  [dynamicKey]: "Developer"
};

console.log(person.job); // Output: Developer
    

In this example, the dynamicKey variable holds the string "job", which is used as a property name in the object.

Getter and Setter Methods

Getter and setter methods allow you to control how properties are accessed or modified. Getters are used to retrieve values, and setters are used to set values.

let person = {
  firstName: "Alice",
  lastName: "Doe",
  get fullName() {
    return this.firstName + " " + this.lastName;
  },
  set fullName(name) {
    let parts = name.split(" ");
    this.firstName = parts[0];
    this.lastName = parts[1];
  }
};

console.log(person.fullName); // Output: Alice Doe
person.fullName = "Bob Smith";
console.log(person.firstName); // Output: Bob
console.log(person.lastName); // Output: Smith
    

In this example, the getter fullName combines the firstName and lastName properties into a single string. The setter allows you to set the firstName and lastName properties by passing a full name as a single string.

Nested Objects

Objects in JavaScript can contain other objects, which are known as nested objects. This allows you to create more complex data structures.

let person = {
  name: "Alice",
  address: {
    street: "123 Main St",
    city: "New York",
    zipCode: "10001"
  }
};

console.log(person.address.city); // Output: New York
    

In this example, the person object contains another object address as a property. To access the nested property, you use dot notation, like person.address.city.

Object Destructuring

Object destructuring is a feature in JavaScript that allows you to extract values from an object and assign them to variables in a clean and concise way.

let person = {
  name: "Alice",
  age: 30,
  job: "Developer"
};

let { name, age } = person;
console.log(name); // Output: Alice
console.log(age); // Output: 30
    

In this example, we use object destructuring to extract the name and age properties from the person object and assign them to variables with the same names.

Summary

Conclusion

Object literals are a fundamental part of JavaScript. They allow you to define and manage objects in a clean, concise, and efficient manner. By mastering object literals, you can create more powerful data structures and write more maintainable JavaScript code.





Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java