DEV Community

Akshat Sharma
Akshat Sharma

Posted on

Day 9 of 30 of JavaScript

Hey reader! Hope you are doing well:)
In the last post we have read about functions and how they are used in JavaScript. Taking our tutorial furthur we are going to study about arrays and different methods used for manipulating arrays.So let's get started...

What are Arrays?

An array is list of similar type entities.
For example we can store a list of 10 numbers in a data structure called array.

Declaration -:
Image description
In the above image you can see that I have used four general methods of declaring an array.
In the first method I have simply declared an array variable and then assigned the list of numbers to it.
In the second method I have made array using Array constructor (those who are not familiar with this term, don't worry we are going to study about it in upcoming blogs). In this I have passed the array entities inside constructor.
In the third method I have created an empty array and then assigned value to each index seperately.
(Note-: We retrieve elements of array using index {arr[0],arr[1],arr[2].....}. Index in array starts from 0 and goes till size of array-1.)
In the fourth method I have made an array using constructor but this time instead of giving entities, I have passed size of array and later in for loop I have assigned value to each index.
We can other methods too for declaring arrays.

Accessing Array Elements-:
You access an array element by referring to the index number.
Image description
So here you can see that we accessing array elements using index.
(Mango is at index 0.
Orange is at index 1.
Kiwi is at index 2.)

Image description
While defining arrays above I have stated that arrays are list of similar type of entities. But above image is clear conflict to this statement. How this above declartion is even possible?
The above declaration is possible because in JavaScript, arrays are not restricted to storing elements of the same type. Unlike some strongly-typed languages, JavaScript arrays can hold elements of any data type, and these elements can be mixed within the same array. This flexibility is part of the dynamic nature of JavaScript.

Adding Array Element-:
The easiest way to add a new element to an array is using the push() method.
Image description
Now here the element is added to the end of the array.But what if we have to add element at some position other than end.We will use following method to achieve this:
Image description
Here we have used splice() method to add two elements after index 0.
array.splice(index, 0, element);
index is the position where the new element should be added.
The second parameter is the number of elements to remove (0 in this case, as we are only adding).
element is the new element to be added.
The splice() method adds new items to an array.

We can also use spread operator(...) to add elements to array-:
Image description
The spread operator is used here to expand the elements of the arr array into a new array newArr. This allows newArr to include 0 at the beginning, the elements of arr in the middle, and 4 at the end. The result is a combined array [0, 1, 2, 3, 4].

Length of array-:
The length property of an array returns the length of an array (the number of array elements).
Image description

So this is it for this blog. I hope you have understood it well. We will continue studying array in the next blog. Till then stay connected and don't forget to follow me :)

Top comments (0)