DEV Community

Ranjith Jr
Ranjith Jr

Posted on • Updated on

js | objects |

Objects


   //Obj name
let person = {

  key     value
 name : "Ranjith",
 age : 21,
 isEmployed: true, 

};

console.log(person);

 name : "Ranjith",
 age : 21,
 isEmployed: true, 

console.log(person.name);

name : "Ranjith",

console.log(person.age);

age : 21


Enter fullscreen mode Exit fullscreen mode

Adding a new property


person.city ="newyork";  //add
console.log(person);

city ="new york" 


Enter fullscreen mode Exit fullscreen mode

Modifying an existing property


person.age =30;  //update
console.log(person);

age = 21;
age = 30;


Enter fullscreen mode Exit fullscreen mode

Object with method


let car ={

  brand:"toyota";
  model:"camry;
  year=2026;

 //functionname
  displayinfo:function(){
               //(this) obj kuulla irukka year eaduthukko
    return '${this.year} ${this.brand} ${this.model};
  }

};

//console.log(car);
console.log(car.displayinfo());

Enter fullscreen mode Exit fullscreen mode

output:

2026 toyota  camry
 // brand:"toyota";
  //model:"camry;
  //year=2026;

Enter fullscreen mode Exit fullscreen mode

let person = {

  key     value
 name : "Ranjith",
 age : 21,
 isEmployed: true, 

};

Enter fullscreen mode Exit fullscreen mode

shorthand: (Destructuring )


let {name ,age,isemployed} = person;   //mela irukka obj key value intha variablesku set agum

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

Output :

Ranjith 
21


Enter fullscreen mode Exit fullscreen mode

Nested Complex Objects


     //obj name
let restaurant = {  //obj 1


name: "Idli Delights",

location: "Chennai" ,

owner: {              // Obj 2

name: 'Rajini Kumar',

age: 50,

contact: {        //  obj 3

   email: 'rajinikumar@sapadusapadu@gmail.com',
   phone: '555-123-4567',

 };

};

menu: [   //Array

{dish: "Masala Dosa, price: 50, spicy: true } ,

{dish: 'Filter Coffee', price: 30, spicy: false },       //obj 4

{dish: 'Pongal', price: 45, spicy: false} ,


], 
} ;


console.log(restaurant);

Enter fullscreen mode Exit fullscreen mode

Accessing properties of the nested objects


console.log('Welcome to ${restaurant.name} in ${restaurant.location}`);

console.log(Owned by ${restaurant.owner.name}, age ${restaurant.owner.age}');

console.log(

Contact: ${restaurant.owner.contact.email), ${restaurant.owner.contact.phone}

);

                 //Array
restaurant.menu.forEach((item) => {

console.log(  
                                            // tarinary operator 
`${item.dish): Rs.${item.price) (${item.spicy? 'Spicy': 'Not Spicy'))

);

});

Enter fullscreen mode Exit fullscreen mode

Destructure owner object


let {

name: ownerName,

age: ownerAge,

contact: { email: ownerEmail, phone: ownerPhone }, } = restaurant.owner;

Enter fullscreen mode Exit fullscreen mode

Output details about the restaurant


console.log(Owned by ${ownerName), age ${ownerAge}`); 

console.log('Contact: ${ownerEmail), $(ownerPhone)`);


Enter fullscreen mode Exit fullscreen mode

Output the menu items using destructuring within forEach


restaurant.menu.forEach(({ dish, price, spicy }) => {

console.log(${dish): Rs.${price) (${spicy? 'Spicy': 'Not Spicy'})`); }); 

Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
thomasbnt profile image
Thomas Bnt ☕

Hello ! Don't hesitate to put colors on your codeblock like this example for have to have a better understanding of your code 😎

console.log('Hello world!');
Enter fullscreen mode Exit fullscreen mode

Example of how to add colors and syntax in codeblocks