Arrays In JavaScript

Arrays In JavaScript

·

4 min read

What are Arrays ?

In JavaScript, arrays are objects which can hold more than one value at once. we can store numbers, strings, boolean values (true and false), characters and objects as value in array.

For Example:

let fruits = ["apple" , "banana", "mango", 4 , 5, True]

Here 'fruits' is an arrays of 6 six items in it.

We can also create array using 'new' keyword . syntax example:

let fruits = new Array('apple', 'banana')

Access and Change Elements of an Array

We can access elements of array with their index numbers. for example think fruits is an array which has few element.

let fruits = ['apple', 'mango' , 'banana'  ]

Now we want access its elements .

fruits[0]    //it has value of  first item = 'apple'
fruits[1]    ////it has value of  first item = 'mango'
fruits[2]    //it has value of  first item = 'banana'

Note : In array index starts with 0 .

Changing elements of Array : Lets think about fruits array . Now I know that In this array first element is 'apple' ,second is 'mango', and third is 'banana'. If we want to change its elements we can simply do this :

fruits[0]    =  'grapes'
fruits[1]    =  'orange'
fruits[2]    = 'Pineapple'

It will change elements of arrays . After doing this in first index of array we will have 'grapes' , in second index we will have 'orange' and third index we will have 'Pineapple'.

Array Properties and Methods

JavaScript arrays has a lot built-in method and properties which makes it easy to work with them and makes our work easy. Most used arrays methods are given below :

1 . array.length property : it gives us length of array. Length of array means how much elements are their in the array. Example


let fruits = ['apple', 'mango' , 'banana'  ]
fruits.length                            // it will give us lenth (3)

2 . concat() : We can join two arrays in one array or we want to join more than two arrays in one array we can do this with concat() . Example :

let fruits1 = ['apple', 'mango' , 'banana'  ]
let fruits2 = ['orange', 'grapes' , 'pineapple'  ]

if we want to add these both arrays in one array we can simpley do this

let fruits = fruits1.concat(fruits2)

//now fruits is one array which looks like this

let fruits = ['apple', 'mango' , 'banana', 'orange', 'grapes' , 'pineapple'    ]

3 . indexOf() : it gives us index number of an item . Example


let fruits = ['apple', 'mango' , 'banana'  ]
fruits.indexOf('mango')    

// it will give us 1 . which is index number of mango . 
//In 0 index we have 'apple'
//In 1 index we have 'mango'
//In 2 index we have 'banana'

4 . includes() : With help of this we can know that if an element exists in array or not . Example


let fruits = ['apple', 'mango' , 'banana'  ]
fruits.includes('banana');       // true
fruits.includes('Cherry');       // false

Because 'banana' exist in fruits array it will return true . and 'Cherry' is not in it fruits array it will return false.

5 . push() : With help of this we can add more items in array . it appends elements in array. Example


let fruits = ['apple', 'mango' , 'banana'  ]
fruits.push('Cherry');       

// it will add 'Cherry' in fruits array
//now it will be like
let fruits = ['apple', 'mango' , 'banana' , 'Cherry']

6 . pop() : It removes the last item from an array and then return it. Example


let fruits = ['apple', 'mango' , 'banana'  ]
fruits.pop();       

// it will remove 'banana' from fruits array
//now it will be like
let fruits = ['apple', 'mango' ]

7 . unshift() : with unshift() method we can add new elements in the array.

Example

let fruits = ['apple', 'mango' , 'banana'  ]
fruits.unshift("orange","pineapple");       

// it will add oragne and pineapple in fruits array

8 . splice() : The splice() method adds and/or removes array elements and overwrites the original array.

Example

const fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
fruits.splice(2, 2);
// it will remove 2 items of index 2   and index three

these are the some most used array method .

Looping through array

Arrays can be looped . we can do it using classical js and with few methods

1. forEach loop: : Calls a funtion once for each array element

2. map() : : Creates a new array by performing some operation . we can modifies element with this method.

3. filter() : : We can filter array elements with this method.

These are the usefull things about an array.