Revision on JavaScript arrays and it's operations.

jagveergagaan

Jagveer Gagaan

Posted on December 7, 2022

Revision on JavaScript arrays and it's operations.

so, I was just thinking of writing something and then this came in my mind about revise the array of javascript and it's operations.
let's start with what is array in javascript and if I put it very simple array is just rack where we store same or distinct things in order, so basically, this is array.

Now let's see declaration of arrays :-

let ourArray = [ "a", "b", "c" ];
Enter fullscreen mode Exit fullscreen mode

Note :- array have index value which help to access the element of array and the start value is always 0(zero).

For Example :-

let ourVariable = ourArray[0];

console.log(ourVariable);
Enter fullscreen mode Exit fullscreen mode

Now, ourVariable have value of "a", which is same that we have at index[0] in ourArray array.

One more thing we can also change value at different index.
Example :-

ourArray[1] = "not b anymore";

console.log(ourArray);

// Expected Output
// [ "a", "not b anymore", "c" ]
Enter fullscreen mode Exit fullscreen mode

Above code show how we can change values at different index locations in Arrays.

Operation on Array

There are mainly four types of operations anyone can do on Arrays which are push(), unshift(), pop() and shift().

let's discuss all of them in detail now firstly we will see push() method.

push() :-

this method adds elements to the end of an array.

Example :-

let ourArray = [ "a", "b", "c" ];

ourArray.push("d");

console.log(ourArray); // output => [ "a", "b", "c", "d" ]
Enter fullscreen mode Exit fullscreen mode

Now, we can see "d" at the end of array list at index[3] and it is because push() method adds element at the end of array.

unshift() :-

It adds elements at the front of array.

Example :-

let ourArray = [ "a", "b", "c" ];

ourArray.unshift("d");

console.log(ourArray); // output => [ "d", "a", "b", "c" ]
Enter fullscreen mode Exit fullscreen mode

Here, in this case, element will get added at the start of array because of unshift() method as it adds element at the start of array and we can see "d" here at the beginning.

pop() :-

as you know by name it help to remove an element from the array but remember it specifically remove element from end of array (or from index[n] ) and if I have to put it simple, you can say that it is totally opposite of push() method.

Example :-

let ourArray = [ "a", "b", "c" ];

let popped = ourArray.pop();

console.log(ourArray);   // output => [ "a", "b"];
console.log(popped);    // output => "c" ;
Enter fullscreen mode Exit fullscreen mode

and the variable "popped" will have value that has be popped from "ourArray".

shift() :-

just like pop() opposite of push() method, here you can assume by name that it might be opposite of unshift() method and you are absolute right.
basically shift() method remove element from the start of an array or can say from zeroth index "index[0]".

Example :-

let ourArray = [ "a", "b", "c" ];

let shifted= ourArray.shift();

console.log(ourArray);   // output => [ "b", "c"];
console.log(shifted);    // output => "a" ;
Enter fullscreen mode Exit fullscreen mode

There are more oprations that someone can perform on array and we will discuss those later in the line of next post.

So, see ya fellas have a nice day : )

💖 💪 🙅 🚩
jagveergagaan
Jagveer Gagaan

Posted on December 7, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related