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.
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'};
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'];
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';
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();
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.
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.
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'
};
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'
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'
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
Certain articles only distributed to subscribers through email