Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 20, 2022 05:51 pm GMT

Did You Know(Part-1):-Somethings about Objects

What is a Object in JavaScript:

Definition by MDN

*Objects in JavaScript, just as in many other programming languages, can be compared to objects in real life. The concept of objects in JavaScript can be understood with real life, tangible objects.

In JavaScript, an object is a standalone entity, with properties and type. Compare it with a cup, for example. A cup is an object, with properties. A cup has a color, a design, weight, a material it is made of, etc. The same way, JavaScript objects can have properties, which define their characteristics.*

Source: link here

In this post I talk about the things that are sometimes new to me or MAYBE you didn't knew about them. And this is Part-1 of the series , hope you like it.

In this I will talk about different ways to do some operations on objects in JavaScript.

How to delete a property in a object:-

1. Using the delete operator:-

It is a special operation that removes a property from an object.

And before I talk about how it is used , Did you know there are two ways to access a object property :-

1.
const obj = {name:'cool'};console.log(obj.name);
2.
const obj = {name:'cool'};console.log(obj[name]);

Now using the delete operator:

const obj = {name:'cool', age:20};delete obj.name;ordelete obj[name]

delete operator is mutable , just a fancy of saying that it modify the object permanently.

2. Using destructuring:-

Destructuring in Javscript is used to unpack values or properties from Arrays or objects.

const obj = {name:'cool', age:20};const {name, age} = obj;

Same way to delete/remove use the syntax:-

const {prop,...restObj} = obj;

const obj = {name:'cool', age:20 , class:A};const {name, ...remainingProp} = obj;console.log(name);console.log(remainingProp);

This way to do this immutable that means the original object remains same as before but still we get access to single property and other remaining properties , in this case name is not present in the remainingProp.

So that's it for this post , thanks for reading and any questions and feedback and any suggestions about Part-2 are welcomed.


Original Link: https://dev.to/lokeshchoudharylc/did-you-knowpart-1-somethings-about-objects-me1

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To