TechMentorAI

object

in JavaScript

Published

An object in JavaScript is like a container that can hold multiple values. English dictionary defines object as 'a thing', 'a person', or 'an entity'. Similarly, in JavaScript, an object is a standalone entity, with properties and type, where properties are essentially variables attached to the object. You can think of it like a person who has properties like name, age, height, etc. and each of these properties has a specific value.

Imagine object in JavaScript

How to Create an Object?

There are several ways to create an object in JavaScript. One of the most common methods is the 'object literal' syntax. It's just opening a pair of curly braces and defining properties inside it. The properties are written as a list, separated by commas, and each property is a key-value pair.

var person = {firstName: 'John', lastName: 'Doe', age: 25, height: '5ft 11in'};

How to Access Object Properties?

Once you have an object, you can access its properties using two ways. One is the 'dot notation', and the other is the 'bracket notation'. In 'dot notation', you write the name of the object followed by a dot and then the property name. In 'bracket notation', you write the name of the object then put the property name inside square brackets. Remember, the property name should be in quotes if you are using bracket notation.

// Dot notation
var name = person.firstName;

// Bracket notation
var name = person['firstName'];

How to Update Object Properties?

Updating the value of a property in an object is as simple as assigning a new value to it. You can use either dot notation or bracket notation for this.

// Update property using dot notation
person.age = 26;

// Update property using bracket notation
person['height'] = '6ft';

What are Methods in Objects?

Objects can also have functions as their properties. These functions are called 'methods' when they are associated with an object. A method is nothing but a function defined inside an object.

// Defining object with a method
var person = {
  firstName: 'John',
  lastName: 'Doe',
  fullName: function() {
    return this.firstName + ' ' + this.lastName;
  }
};

// Accessing method
var name = person.fullName();

No Time to Read? Learn on the Go!

By reading this article, you've invested 1.43 minutes of your life into expanding your knowledge and perspectives. Now, imagine learning on-the-go, turning every moment into an opportunity for growth and discovery.

You know how in real life, we have things like cars, dogs, or your favorite toy? In JavaScript, an object is just like one of these real things, but it lives inside the computer. Each object can have different details about it. These details are called properties. For example, a car object might have properties like color and size.

Imagine object in JavaScript

How Do We Make an Object?

We can make a new object in JavaScript using something we call curly braces '{}'. It's a little like building a toy from a set. We tell JavaScript what details (or properties) we want our object to have.

Example of an Object

Let's create a toy car object. This toy car has a color and a size. In JavaScript, we can represent this toy car as an object like this:

var toyCar = {
 color: 'red',
 size: 'small'
};

Using Our Object

Once we've made our object, we can ask JavaScript questions about it, like 'What is the color of the toy car?'. JavaScript would then tell us that the color of the toy car is 'red'.

console.log(toyCar.color); // This will display 'red'

More About the Object

You can even change the properties of an object. If you want your toy car to be green instead of red, you can do so by 'painting' it:

toyCar.color = 'green'; 
 console.log(toyCar.color); // This will display 'green'

No Time to Read? Learn on the Go!

By reading this article, you've invested 1.04 minutes of your life into expanding your knowledge and perspectives. Now, imagine learning on-the-go, turning every moment into an opportunity for growth and discovery.

If you want to know more, you can read about JavaScript Loops or JavaScript Arrays Both of these are very important concepts in using JavaScript effectively.

About author

Roman Y.
Senior Software Engineer at Nike

Why did I decide to launch this website? Drawing from my rich background in interviewing candidates for a variety of developer roles, I've observed a common pattern: many applicants share similar deficiencies in their knowledge during technical interviews. Motivated by this insight, I established this website with the aim of assisting developers in securing their ideal job. Through straightforward and concise articles, my goal is to not only deepen your understanding of programming language nuances but also to equip you with the insights needed to deliver the precise answers interviewers expect. Essentially, you'll be providing the correct response. I encourage you to spread the word about this site on social media platforms. Echoing the wisdom of an Armenian saying: "Do good and cast it into the water."

EXCLUSIVELY

Certain articles only distributed to subscribers through email