What is Object Literal?

Object Literals in JavaScript

If you’re new to JavaScript programming, you may have heard the term “object literal” thrown around a lot. But what exactly is an object literal, and how does it work?

Introduction

In JavaScript, an object literal is a syntax for creating new objects. It’s a convenient way to define and initialize an object in a single line of code. Object literals enclose key-value pairs in curly braces {}, where the key represents the property name as a string, and the value can be any valid JavaScript expression

Syntax of Object Literals

Here’s an example of an object literal in JavaScript:

let myObject = {
  property1: value1,
  property2: value2,
  property3: value3
};

In this example, we have defined a new object called myObject. It has three properties, property1, property2, and property3, each with a corresponding value value1, value2, and value3.

Creating Object Literals

Object literals can be created in various ways. Here are some examples:

Object Literal Syntax

let person = { name: 'John', age: 30 };

the Object Constructor

let person = new Object();
person.name = 'John';
person.age = 30;

Using Object.create Method

let person = Object.create(null);
person.name = 'John';
person.age = 30;

Accessing Object Properties

Once you have created an object literal, you can access its properties using either dot notation or bracket notation.

Dot Notation

console.log(person.name); // Output: John
console.log(person.age); // Output: 30

Bracket Notation

console.log(person['name']); // Output: John
console.log(person['age']); // Output: 30

Modifying Object Properties

You can modify the values of object properties using either dot notation or bracket notation.

Dot Notation

person.age = 40;
console.log(person.age); // Output: 40

Bracket Notation

person['age'] = 40;
console.log(person['age']); // Output: 40

Adding Object Properties

You can add new properties to an existing object using either dot notation or bracket notation.

Dot Notation

person.address = '123 Main St';
console.log(person.address); // Output: 123 Main St

Bracket Notation

person['address'] = '123 Main St';
console.log(person['address']); // Output: 123 Main St

Deleting Object Properties

You can delete properties from an object using the delete operator.

delete person.address;
console.log(person.address); // Output: undefined

Object Methods

In addition to properties, objects can also have methods. Methods are functions that are associated with an object.

Here’s an example of an object with a method:

let person = {
  name: 'John',
  age: 30,
  sayHello: function() {
    console.log('Hello, my name is ' + this.name + ' and I am ' + this.age + ' years old.');
  }
};

person.sayHello(); // Output: Hello, my name is John and I am 30 years old.

Conclusion

Object literals are a useful and concise way to define and create objects in JavaScript. They are created using curly braces {} and consist of one or more key-value pairs. Properties and methods can be added, modified, and deleted using various notations.

Follow Us on

https://www.linkedin.com/company/scribblers-den/
https://www.facebook.com/scribblersden.blogs

Read More

https://scribblersden.com/what-is-destructuring-object/

Related Post

Leave a Reply

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