Unveiling the Core: Objects in JavaScript

Eduardo Kohn
3 min readOct 16, 2023

If you haven’t set up your environment yet, click here to read how to get started with Node.js on MacOS using NVM. Now that you have your development environment ready, it’s time to delve deeper into the fascinating world of JavaScript.

As we embark on this leg of the journey, we shall unveil the core concept that propels JavaScript into being one of the most flexible and widely-used programming languages — Objects.

Understanding objects is like holding the golden key to the many doors within the JavaScript kingdom, whether you are a frontend developer or venturing into the realms of Node.js.

Why Objects?

In JavaScript, almost everything is an object or behaves like an object, making them the cornerstone of the language. Objects are collections of key-value pairs, where a key is a string, and the value could be a string, number, boolean, function, array, or even another object. This versatile nature of objects provides a solid foundation for modeling and organizing code in a structured, reusable, and manageable way.

Creating Objects

Creating objects in JavaScript is straightforward. There are multiple ways to create objects, each with its own use case. Here’s a simple way to create an object using object literal syntax:

const car = {
brand: 'Toyota',
model: 'Corolla',
year: 2020,
startEngine: function() {
console.log('Engine started');
}
};

In this example, car is an object with properties brand, model, and year, and a method startEngine.

Digging Deeper

JavaScript objects are dynamic, meaning you can add or remove properties and methods even after an object is created. For instance:

car.color = 'Blue';
delete car.year;

Now, the car object has a new property color, and the property year has been removed.

Objects and Functions

In JavaScript, functions are first-class objects. They can have properties and methods, be assigned to variables, passed as arguments, and returned from other functions. This feature unlocks a high degree of flexibility and expressiveness.

function greet(name) {
return 'Hello, ' + name;
}

greet.language = 'English';

Here, greet is a function object with a property language.

Arrays and Primitives as Objects

Surprisingly, even arrays and primitive types (numbers, strings, and booleans) exhibit object-like behavior in JavaScript, thanks to their respective prototype objects. This concept underpins the type-coercion and prototype-inheritance mechanisms in JavaScript.

const num = 10;
console.log(num.toString()); // "10"

In this snippet, num is a number, but it's temporarily converted to an object to access the toString method.

By demystifying objects, we’ve taken a significant leap in our JavaScript journey. Objects’ omnipresence and their ability to model real-world entities make JavaScript a powerful and expressive language for both client-side and server-side development. However, to truly harness the power of JavaScript, it’s essential to understand the underlying building blocks of the language.

As we move forward on this voyage, we’ll delve into the bedrock of JavaScript: variables and data types. How are they defined? How do they behave? And most importantly, what’s the difference between primitive and reference types?

So, keep experimenting and sharpen your curiosity. Get ready to delve deeper into the intricacies of data storage and manipulation in JavaScript.

Continue to our next tutorial on JavaScript Building Blocks: Variables, Primitive Types, and Reference Types!

Happy Coding! 🚀

--

--

Eduardo Kohn

I'm Eduardo Kohn, crafting efficient software since 2016. A heavy metal fan, dog lover, and surf enthusiast, I thrive on learning and technological rigor.