Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is an object in JavaScript

Understanding Objects in JavaScript

JavaScript, as a language, has a feature called "objects." These objects allow us to organize and manage our code in a more efficient way. But what exactly are these objects? Let's find out.

What is an Object?

An object in JavaScript can be seen as a collection or a container that holds related data and functionality. This container has "properties" and "methods." You can think of properties as characteristics of the object, and methods as actions that the object can perform.

To make this more intuitive, think of an object as a bag. The bag (the object) can contain many different things (properties), like a book, a pen, or a notebook. It can also perform actions (methods), like opening the bag to access its contents, or closing it to secure those contents.

var bag = {
    book: 'JavaScript: The Good Parts',
    pen: 'Blue',
    notebook: 'A4 size',
    open: function() {
        console.log('Opening the bag');
    },
    close: function() {
        console.log('Closing the bag');
    }
};

In the above example, book, pen, and notebook are properties of the bag object, and open and close are methods.

How to Create an Object?

There are different ways to create an object in JavaScript. The simplest and most common way is using the object literal syntax, as shown in the previous example. We can also use the new keyword, or Object.create() method.

// Creating an object using the 'new' keyword
var bag = new Object();
bag.book = 'JavaScript: The Good Parts';
bag.pen = 'Blue';
bag.notebook = 'A4 size';

// Creating an object using 'Object.create()' method
var bag = Object.create(Object.prototype);
bag.book = 'JavaScript: The Good Parts';
bag.pen = 'Blue';
bag.notebook = 'A4 size';

Accessing Object Properties and Methods

Accessing the properties and methods of an object is straightforward. We can use either the dot notation (.) or the bracket notation ([]).

console.log(bag.book); // Output: 'JavaScript: The Good Parts'
console.log(bag['pen']); // Output: 'Blue'

bag.open(); // Output: 'Opening the bag'

The Power of Objects

JavaScript objects are powerful because they can contain other objects or even functions. This feature allows us to create complex structures and manage them efficiently.

For example, consider an object called university that has properties like name, location, and departments. Here, departments can be an array of department objects, and each department object can have properties like name, head, and students, where students can be an array of student objects, and so on.

var university = {
    name: 'Harvard University',
    location: 'Cambridge, MA',
    departments: [
        {
            name: 'Computer Science',
            head: 'Prof. XYZ',
            students: [
                {name: 'John Doe', age: 20},
                {name: 'Jane Doe', age: 22}
            ]
        },
        // More departments...
    ]
};

Conclusion

In the vast ocean of JavaScript, objects are like islands, grouping related data and functions together, making our code organized and easier to navigate. They are one of the fundamental aspects of JavaScript, and understanding them is key to mastering the language.

So the next time you pack your bag (object), remember to carefully select what to put inside (properties), and what actions you want your bag to perform (methods). Happy coding!