JavaScript - Learning About Objects

in #hive-1635212 years ago

js-object.png

In my previous post, I shared my experience learning the basics of JavaScript, a lot of new things, and also things I had to learn about the basics of JavaScript, some of which I just remembered.

The next material on the introduction of data structures really requires concentration and is difficult, because there are many new things that I have never heard of before.

I learned a few things about data structures such as;

  • Object
  • Array
  • Map
  • Set

So, here I only conclude the things that I consider important as my documentation in learning about Objects in JavaScript because the material taught is also very dense.

On the next occasion, I will share Arrays, Maps, and Sets as my notes in learning this material.

Objects

An object is a data type. Objects can store values of various data types and form more complex data.

Writing Objects can be done by putting data in {}.

cont user = {key1: "value1", key2: "value2", key3: "value3", key4: "value4" };

Other examples of Objects are;

const hiver = {
    name: "Iqbal Adan",
    join: "November 2017",
    species: "Human",
};

The difference in writing code as above will have no effect because the new line will not affect anything, so writing like the second example above will be better because it will be neater and orderly so that it is easier for us to read and understand the data from the created object.

The code above will not show s anything, to display what we have created, we need to call it with the command console.log;

const hiver = {
    name: "Iqbal Adan",
    join: "November 2017",
    species: "Human",
};

console.log(`Halo, nama saya ${hiver.name}`);
console.log(`Saya bergabung di Hive sejak ${hiver.join}`);

/* output
Halo, nama saya Iqbal Adan
Saya bergabung di Hive sejak November 2017
*/

output hiver.png

Object Modification

You can modify an object by using the assignment operator (=). Now let's try to create a new object;

const spaceship = {
    name: "Millenium Falcon",
    manufacturer: "Corellian Engineering Corporation",
    maxSpeed: 1200,
    color: "Light gray"
};

I will change the color from Light gray to Glossy red, so I just assign the spaceship key using the dot operator to change the color property, so that it becomes Glossy red, as well as the speed, from 1300 to 1200;

spaceship.color = "Glossy red";
spaceship["maxSpeed"] = 1300;
console.log(spaceship);

/* output
{
  name: 'Millenium Falcon',
  manufacturer: 'Corellian Engineering Corporation',
  maxSpeed: 1300,
  color: 'Glossy red'
}
 */

Deleting property within an object

To delete object properties, you can use the delete keyword keyword

const spaceship = {
    name: "Millenium Falcon",
    manufacture: "Corellian Engineering Corporation",
    maxSpeed: 1200,
    color: "Light Gray"
};


spaceship.color = "Glossy red";
spaceship["maxSpeed"] = 1300;

delete spaceship.manufacture;

console.log (spaceship);

delete-object.png

Conclusion

In conclusion, an object can be categorized as a variable with more than one property and value.

The material about this object is new to me, it takes time for me to digest and understand it, hopefully, this little note can be my reference in recalling the material about this object later, see you in the next post.

Sort:  

Good! you are doing great ! it is interesting that in JS all types of variables we use const! LOL
!1UP

Thanks, man. It's getting harder day by day to absorb new subject :D

Don-1UP-Cheers-Cartel-250px.png

You have received a 1UP from @gwajnberg!

The following @oneup-cartel family members will soon upvote your post:
@stem-curator, @vyb-curator, @pob-curator
And they will bring !PIZZA 🍕

Learn more about our delegation service to earn daily rewards. Join the family on Discord.