DEV Community

Jacob Stern
Jacob Stern

Posted on

3

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!

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay