In the last segment, you learnt about arrays, and how useful they are in storing multiple values. But can you use arrays for storing information about a variable that might have multiple characteristics? For example, a car can have multiple properties such as seating capacity, engine power, price, etc. Storing all this information in an array for a lot of cars is quite problematic. Let’s find out what the problem is in the next video.
So this is interesting, you heard about objects, and they seem like a solution to the problem when you want to store multiple properties of something. Let’s learn more about objects in the next video.
Objects are just a collection of variables lumped together in key-value pairs. Objects store their elements as key-value pairs. Each key-value pair is termed as a property. The key name should be a JavaScript identifier but you can break this and not have an identifier as a key. In such a case, you should enclose the key name in quotes or they will throw an error.
Objects are used to store information about almost every single entity. Objects are the custom data types in JavaScript because it depends upon the user what value he/she wants to enclose within an object. Like variables and arrays, objects are also declared using the var keyword in JavaScript.
A similar example would be a piece of information about a hotel. So if you have to store the information, you can easily make an object like the one given below.
var hotel = { numberOfRooms : 12, location : "Pune", stars : 3 };
Note that the objects in JavaScript are way too different as compared to the objects in most of the other Object-Oriented Programming languages.