DEV Community

Kavin Loyola S
Kavin Loyola S

Posted on • Edited on

Object in JS

Object

Objects are variables that can store both values and functions, and it is a collection of properties. Methods are actions that can be performed on objects.

const person = {
    name: "Kavin",
    age: 22,
    Dept: "CSE"
};
console.log(person.name);

0/p - Kavin

Enter fullscreen mode Exit fullscreen mode

Properties

A JavaScript object is a collection of properties, and Properties can be changed, added, and deleted. there are some ways to access the object properties in these ways :

  • Dot Notation

  • Bracket Notation

  • Expression

Methods

Methods are actions that can be performed on objects, and Methods are functions stored as property values.

const person = {
  name: "Kavin",
  age: 21,
  info: function() {
      console.log("name");
  }
};
person.info();
console.log(person.name);

o/p - name
      Kavin

Enter fullscreen mode Exit fullscreen mode

CRUD

const mobile = {
     brand: "Realme",
     ram : 16,
     storage : 128,
     price : 20000
};
mobile.model = 9;
console.log(mobile);
mobile.ram = 8;
delete mobile.price;
console.log(mobile);

o/p - { brand: "Realme", ram : 16, storage : 128, price : 20000 }
      {brand: 'Realme', ram: 8, storage: 128, model: 9}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)