DEV Community

Jacob Stern
Jacob Stern

Posted on

Day 62 / 100 Days of Code: Objects are the JavaScript Building Blocks

Sat, August 31, 2024

Hello, fellow coders! 🌟

Today was a big day in my JavaScript journey as I tackled the biggest assignment in the JavaScript Syntax II lesson: Objects.

We’ve been using methods, like Math.floor() in random number generation, where floor() is the method that returns the lower integer value when rounded down. So, it’s good to see how methods are created. Objects are collections of properties in the form of key-value pairs, such as this one named spaceship, with homePlanet Earth:

const spaceship = {
  'Fuel Type': 'Turbo Fuel',
  homePlanet: 'Earth',
  mission: 'Explore the universe'
};
Enter fullscreen mode Exit fullscreen mode

Object properties can be accessed using either dot notation or bracket notation. You can add a property to an object using assignment or delete one using the delete keyword:

spaceship.color = 'glorious gold';
spaceship.numEngines = 5;
delete spaceship['Secret Mission'];
Enter fullscreen mode Exit fullscreen mode

When an object’s properties include a function, this is called a method. Unlike standard properties, methods are not what it is, but what it does, such as in the method invade:

spaceship.invade = () => {
  console.log('Hello! We have come to conquer your planet. Instead of Earth, it shall be called New Xaculon.');
};
spaceship.retreat = () => {
  console.log('We no longer wish to conquer your planet. It is full of dogs, which we do not care for.');
};
Enter fullscreen mode Exit fullscreen mode

Objects are the core of JavaScript, so it's good to get a handle on them!

Top comments (0)