Dive deep in the ocean of JS-1
Ohhhh…. Objects is it? ‘Yeah I know, But I’m not sure’.
I think most of us in the same boat.
We know what is Object in Javascript, But we are not pretty clear about the behaviour of this guy.
Am I right?..
If yes…? Then, come on…
Let’s explore.
Dear friends, This will be series of blogs where we dive deep in to the concepts of Javascript objects.
As we all might be working on the latest tech stack of Javascript.
It might be React JS,Angular,Node JS and so on…
We will be pretty familiar with those frameworks and their architecture.
Meanwhile we should be also strong enough on the building block of those frameworks. i.e., Javascript.
Okay… Now we will look in to what is Object in Javascript.
What is Object?
“In Javascript, Everything is an Object”.
Okay…. okay…. I can read your mind, You are asking me that “Everybody tell the same, But justify me ?”.
Is it?
Yeah, will try to convince.
First I will make sure that, Object is nothing but collection of key and value pairs.
How we denote object ?
let obj = {name: ‘John Doe’, occupation: ‘Engineer’}
Above example is a collection of key and value pair, where name & occupation are Keys and John Doe & Engineer are Values for those keys respectively.
From now we will address Key as Property of an Object.
Now if i want the to get the name property from the object obj.
we will do obj.name. is it?
- Yeah. It’s right.
Now keeping this in mind, we will analyze the things to justify that everything in JS is an Object. Will start with Arrays first.
How Array is an Object?
If I want to create array of numbers, then I can create with two different approaches.
- let array = [1,2,3,4];
- let array = new Array(1,2,3,4);
But now if you print these two arrays:
you get [1,2,3,4] as an output.
Now I have a question like, How can I find the length of the array?
- Yeah, very simple right? array.length will return the value 4.
I think now you might get a question like,
Hey, I assigned the value [1,2,3,4] to the variable array, not the object.
Still from where .length property is coming from? How that .length property holds good for the array I have created? How its returning the exact length of the array that we have created?
Yeah, Here we go…..
Actually, Based on the value what you assign, Javascript interprets the type of value and it creates an instance of that function object.
for eg.,
if you do, let array = [1,2,3,4] implicitly it will be like let array = new Array(1,2,3,4).
It means I’m instanciating the Object of type Array,
Where Array Object has so many properties where some properties assigned with values and some with functions.
To access the different properties of the Array, we will go with object traversal like.
Find the length : array.length
Filter the data: array.filter
……
It means [1,2,3,4] is an array of numbers instanciated from Array() in the form of object, where that object has some properties to process the Array. like mentioned above.
Any entity which follows the notation objectName.property refers to an object.
So for an array I’ll do all such operations using the properties of Array object.
Hence Array is an Object.
We will discuss about other datatypes in later blogs.
Thank you.
Happy Scripting.