Michael Solati
JavaScript Objects

JavaScript Objects


This blog post is your crash course for you to learn about one of the vital topics in JavaScript, i.e., JavaScript Objects.

JavaScript Meme

There are two ways JavaScript data can be defined either a Primitive or an Object. Objects are what most developers interact with. Some Objects you may be familiar with are:

JavaScript objects are mutable, and their values can be changed. Objects can have properties and methods as well. It’s crucial to remember JavaScript is designed on a simple object-based paradigm.

Objects are collections of properties, and properties are an association between a key and a value. In some cases, though, the value of a property is a function, making that property a method. The properties of an object usually describe the characteristics of its variable. For example, an Array has .length to know how many elements are in them. The Math object has a .PI property, in case you get hungry.

First Code Snippet

Methods behave differently, as they’re functions that need to be called. They can be used to modify or convert a property of an object, perform an action, return a specific piece of information, and more. For example, if you’ve worked with a String, you may have called the toUpperCase() method to get the string in complete upper case.

Likewise, with an Array, you could call .sort() to sort the elements.

Second Code Snippet

There are different ways to create an Object in JavaScript. For example, you could make an Object using an object initializer or create a constructor function and instantiate a new instance of that object. Object initializers are creating objects with literal notation. This is consistent with the terminology used by C++. Using object initializers This is wildly different from creating an Object using a constructor function where we can create reusable and distinct instances of an object. To create this type of object, start with creating a function where the properties and methods are added to the this object.

Using a constructor function

You can then instantiate a new instance of that object with new and reuse it repeatedly.

Instantiating dog objects

This blog is inspired by one of our Twitter threads that we post on our Twitter account. There are many threads like this; you should check them out.