Today is the 13th day of my #100DaysOfCode journey with JavaScript.
I write about my learnings in an explained way through my blogs and socials. If you want to join me on the learning journey, make sure to follow my blogs and social and share yours too. Let’s learn together!🫱🏼🫲🏼
This Article is a part of the JavaScript Fundamentals series.
Objects
In JavaScript, almost “everything” is an object. JavaScript objects start with an open curly-brace { and end with a closed curly brace }. It contains key-value pairs in between these braces.
const team = {
name: "India",
wins: 91,
inFinals: true,
};
In above object team, we have three keys: name, wins and inFinals. The value associated with name is "India", with wins is 86 and with inFinals is true.
Example: Let’s create an object representing a pizza order! In the order object, add the following three keys with values accordingly:
pizzas - Any number greater than zero.
extraCheese - A boolean. Either true or false.
deliveryInstructions - Any string of instructions.
const order = {
pizzas: 5,
extraCheese: true,
deliveryInstructions: "Keep it medium",
};
Retrieve Values
Now we will retrieve values from object. From the given code, If we wanted to retrieve the name of the team, we can do this in two ways:
const team = {
name: "India",
wins: 91,
inFinals: true,
};
console.log( team.name ); // India
console.log( team['name'] ); // India
Brackets [] or . property accessor operator can be used, similar to arrays!
Array of Objects
Now, we will see what happens if we put objects inside arrays and vice versa.
Let’s take our team example again:
const team = {
name: "India",
wins: 91,
inFinals: true,
};
What if we have multiple teams:
const teams = [India, Australia, England];
for(let i = 0; i < teams.length; i++) {
console.log(teams[i].name);
}
This example loops over each team and logs out the name of each team.
Example: Given an array of pizza orders, return the total number of pizzas ordered. The orders are an array of objects, each with pizzas key inside.
function numberOfPizzas(orders) {
let total = 0;
for(let i = 0; i < orders.length; i++) {
total += orders[i].pizzas;
}
return total;
}
Conclusion
Ending with an extra bit of information about JavaScript…
A JavaScript object is a state-and-behaviour-containing entity (properties and method). Examples include a car, pen, bicycle, chair, glass, keyboard, and monitor. JavaScript is an object-based language. In JavaScript, everything is an object. JavaScript relies on templates rather than classes. To obtain the object in this case, no class is created. But we deliberately make objects.
Today I learned about Objects in JavaScript.