DEV Community

Cover image for Arrays and Objects in Js!!! Explained [may be] Simplest way possible.
Himanshupal0001
Himanshupal0001

Posted on

Arrays and Objects in Js!!! Explained [may be] Simplest way possible.

Hey devs, Today we gonna talk about Arrays.

Image description

If you have some experience with c, c++ or java you would have know that array is a homogeneous list of values. Now Lets break it down. Homogeneous means same and list is just a list. So list of same thing is called array of homogeneous. For example array of dog breeds. We have a list of different dog breeds but at the end dogs are dogs. Let dog be the datatypes and breed is the value. Like [2,4,6,34,54,3,25,8] this array of integer has different values but belong to one type of datatype group which is integers.

But in Js we can have heterogeneous array. Heterogeneous means different types. So we can make a list of animals like [dog,cat,fruit,car,chair] anything. For example [1,3,"str1", "o2", true, null, undefined] this array consists of number, string, boolean, null and undefined datatypes. It is more similar to lists in python.

Why we need Arrays ?

We need array for list of data for a particular type. For example if we want to make forms in an application, we can use arrays and so on.

Lets talk about Objects now

I bet you read the definition of object before:

Any real world entity which can have some characteristics or which can perform some tasks is called as Object. This object is also called an instance i.e. a copy of entity in programming language. ... Each object can be different based on their characteristics.

But is doesn't make any sense right? So what are objects to be exact?

In programming language we basically doing real world things technically. So eventually these programs interacting with real world. Lets understand objects with following example.

I believe we must have written an essay on animal or pet right? If you haven't I highly insist to write an essay on an animal or about your pet.

Image description

Suppose I wrote an essay on my dog. So I will write about he's properties and nature right? Like it has 4 leg, two eyes , one nose, one tails , about color and breed extra. Also gonna talk about how cute and good boy he is. So these all are his properties and it all associated to one common thing "Dog". Objects are likewise. We define a variable , explain properties and then associate with an object variable. For example:

function properties(legs, eyes, tail, nose) 
{
   this.legs = legs;
   this.eyes = eyes;
   this.tail = tail;
   this.nose = nose;
}

 let dog = new properties(4, 2,1,1);//the values assign to the arguments of the function. (****function(argument1, argument2 ....)
//Don't get confuse here
//dog is a variable 
//we created a object same name as function name//also call as constructors 
//we assigned the object to the dog variable
//we calling value of properties using dog variable (and (.) operator)

console.log(dog.legs, dog.eyes, dog.tail, dog.nose);
Enter fullscreen mode Exit fullscreen mode

That's how we can access different part of an entity with objects.

Think it of like this.
We have a phone and in phone we have screen, ram, battery, speaker, processor etc. Now we will modify this phone.

  1. Phone.screen.Amoled.:--> we have a phone->which has a screen ->we fixed a Amoled screen instead of IPS.

  2. Phone.ram.8: --> we have a phone->which has a 4gb ram -> we upgraded to 8gb ram.

That's how you can pass value anytime and modify your phone.

Image description

I hope you understand the concept of array and objects.

Top comments (0)