Categories: Javascript Tutorial

Types of Objects in Javascript

In JavaScript, an object is a data structure that allows you to store collections of data and more complex entities. Objects are key-value pairs, where each key (also called a property) is associated with a value. The values can be of any data type, including other objects and functions.

Creating Objects

Object Literals

The most common way to create an object is by using an object literal:

let person = {
 
name: "John",
age: 30, 
isStudent: false, 

greet: function() { 

console.log("Hello, my name is " + this.name); 
}
};

Using the new Object() Syntax

Another way to create an object is by using the Object constructor:

let person = new Object(); 

person.name = "John"
person.age = 30
person.isStudent = false

person.greet: function() {
console.log("Hello, my name is " + this.name); 
}

Object of a class

We can also create object of a class and we have seen that example in our last session

class Person {
  
  }

let person = new Person();

person.name = 'John';
person.age = 30;

console.log(person.name);
console.log(person.age)

Object of a function

We can also create object of a function because there was no concept of class in the earlier version of javascript. ES6 version of javascript introduced classes.

function person() {
  
  }

let p = new person();

p.name = 'John';
p.age = 30;

console.log(p.name);
console.log(p.age)

Accessing Object

console.log(person)

Accessing Objects using Dot Notation

console.log(person.name)
console.log(person.age)
console.log(person.isStudent)

Accessing Objects using Bracket Notation

console.log(person["name"])
console.log(person["age"])
console.log(person["isStudent"])

Accessing a function inside an object

person.greet()

Accessing the keys, values and entries of an object

console.log(Object.keys(person))
console.log(Object.values(person))
console.log(Object.entries(person));

Modifying Properties

person.name = "Jack"
person["age"] = 31

Adding Properties

person.address = "123 Main Street"

Deleting Properties

delete person.isStudent;

for in loop

for (let key in person) {
  console.log(key + ": " + person[key]);
}
Admin

Working Professional with more than 12 Years of Experience in Software Testing with Automation and Manual Testing Knowledge.

Recent Posts

Mostbet bahis sitesindeki kazancli oyunlar

Mostbet bahis sitesindeki kazancli oyunlar

55 years ago

Mostbet Turkey: Profesyonel Musteri Destegi

Mostbet Turkey: Profesyonel Musteri Destegi

55 years ago

Heyecan Verici Kazanclar icin Gates-of-Olympus Slot

Heyecan Verici Kazanclar icin Gates-of-Olympus Slot

55 years ago

What is Method and Difference between Functions and Methods in Javascript

Difference between Functions and Methods in Javascript Before discussing the difference between a function and…

55 years ago

Objects, Classes and Constructor in Javascript

Objects, Classes and Constructor in Javascript Classes and Objects A class in any programming language…

55 years ago

OOPs in Javascript

We will start from object oriented programming basics like What are different concepts in Object…

55 years ago