Object is a data type used to support Object oriented programming in Javascript. Objects in Javascript are analogous to HashMap in Java or Dictionary in Python.
In object oriented programming, every Object is said to have some properties and some behavior. For e.g. if there is an Object called “Dog”, then its properties can be its “name”, “age”, “color”, etc. Similarly its behavior can be “to walk”, “to run”, “to bark”, etc.
In Javascript, these Properties are the Variables inside the Object and the Behaviors are the Methods.
You can define Javascript keyword using the Object literals:
E.g.
const car = {
name: “Hyundae”,
engine: “petrol”,
price: 100000,
color: “white”
}
Here, the properties can also be referred to as “key: value” pairs.
In Javascript Objects, you can have only unique keys. Though you can have duplicates in values.
You can also define an empty or partially created object and can assign new key value pairs later.
For. e.g.
const car = {
name: “Hyundae”,
engine: “petrol”,
}
car.price = 100000;
car.color = “white”;
To access or read back the value of any property, you can use two types of syntax:
object.property_name
or
object[property_name]
E.g.
console.log(car.price)
or
console.log(car[“price”])
The second method is especially useful in case of property name with any special characters.
We shall explore about the Methods in Objects in future tutorials.
I hope this tutorial gave you a good understanding of Objects in Javascript and their usage.
Fully customizable CRM Software for Freelancers and Small Businesses