Everything is Object in JS
What are Objects ?
π Let's forgot about JS objects and talk about real world objects , every object in real world have certain properties and behaviors.
πFor Example : Girlfriend π , yes ! girlfriend is object , girlfriend have properties like hairColor,eyeColor,name,number,address,likes,dislikes etc...
π In JavaScript Object can be defined as group of related properties.
π Grouping all the related properties to one thing (ex. Car, GF) makes much more sense , because now it's all stored in one place and is easy to access.
π Objects come under category of composite data type , which means mix and match of primitive data types.
How Objects work?
π as we can see we grouped all the related properties under one variable namely girlfriend
πAn object is a collection of properties, and a property is an association between a name (or key) and a value.
Before making object keep in mind
π Objects always work in key value pair [eg. name:"john"]
π Arrays are always objects in JS.
π Functions are always objects in JS.
π Objects are always embedded in curly braces {}
Syntax
var object = {
property : value,
property2: value,
....
}
The Girlfriend Object π
let girlFriend = {
name : "alexa",
surname : "grace",
likes: ["reading","football"],
number : 123456789
}
π We can access object properties using "."
π For Example girlFriend.name
gives output "alexa"
π girlFriend.likes
gives us array ["reading","football"]
as output
Let me know in comment section if you have any doubt or feedback. it's always worth to give time to thriving developer community :)
Keep Coding β€
Top comments (0)