Javascript array fundamentals

Javascript array fundamentals

What is an array in javascript

In variable we can store only one value or literal and if we want to store multiple items in single variable then the concept of array comes in. An array is an object which can contain elements of any datatype like string ,number ,boolean,Objects, and also other arrays in javascript.

How to create an array

Array can be created by many ways :-

const cities = ["Jaipur" , "Delhi " , "Mumbai" ,"Chandigarh"];
console.log(cities);

image.png

const cities2 = new Array('Jaipur', 'Mumbai');
console.log(cities2);

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1662362357562/mfoN81P7F.png align="left")

Accessing arrays

We can access the array by using index number of position in an array.

const cities = ["Jaipur" , "Delhi " , "Mumbai" ,"Chandigarh"];
console.log(cities[0]);

image.png

Methods in arrays

1.Push():-

Using push() method,we can append a new element at the end of an array.

const cities = ["Jaipur" , "Delhi " , "Mumbai" ,"Chandigarh"];
cities.push("Madhya pradesh");
console.log(cities);

image.png

2. pop():-

using pop() method we can remove the last element from array.

const cities = ["Jaipur" , "Delhi " , "Mumbai" ,"Chandigarh"];
cities.pop()
console.log(cities);

image.png

3. includes():-

It will check if a certain item is present in an array or not. It will return a boolean value .

const cities = ["Jaipur" , "Delhi " , "Mumbai" ,"Chandigarh"];
cities.includes("Delhi");  //true
cities.includes("Chennai");  //false

We can also use indexOf()to find the index of the element if present. If it is not present in an array it returns -1.

Syntax

indexOf(searchElement)

indexOf(searchElement, fromIndex)

4. shift():-

It removes the front element from array and shifts remaining array to the left and it returns the returns the removed value.

const cities= ["Jaipur" , "Delhi " , "Mumbai" ,"Chandigarh"];

const firstElement = cities.shift();

console.log(cities);
console.log(firstElement);

image.png

5. unshift():-

It adds the new element to the beginning of an array and shifts the remaining array to the right. The unshift() method overwrites the original array.

const cities= ["Jaipur" , "Delhi " , "Mumbai" ,"Chandigarh"];

const firstElement = cities.unshift("Chennai", "Bangalore" );

console.log(cities);
console.log(firstElement);

image.png

6. slice():-

Syntax

slice()
slice(start)
slice(start, end)

It will return a portion of array copy into a new array object. It does not change the original array.

const cities= ["Jaipur" , "Delhi " , "Mumbai" ,"Chandigarh"];
const elements = cities.slice(1, 3);
console.log(elements);

image.png

7.sort():-

It sorts the elements of an array in place and returns the array. The default sort order is ascending.

const cities= ["Jaipur" , "Delhi " , "Mumbai" ,"Chandigarh"];

console.log(cities.sort());

image.png

8. concat():-

The concat() method is used to join two or more arrays together.the original arrays remain unchanged.

const cities =  ["Jaipur" , "Delhi " , "Mumbai" ,"Chandigarh"];
const moreCities = ["Chennai", "Gurugram"];
const combined = cities.concat(moreCities);
console.log(combined);

image.png

9. copyWithin():-

The copyWithin() method copies part of an array to another location in the same array and returns it without modifying its length.

Syntax

copyWithin(target)
copyWithin(target, start)
copyWithin(target, start, end)

In this target means the location where to copy the element of an array and start means the index from which to start copying elements from. If negative, start will be counted from the end.

If start is omitted, copyWithin()will copy from index 0. endmeans the index at which to end copying elements from. copyWithin copies up to but not including end. If negative, end will be counted from the end.

const cities =  ["Jaipur" , "Delhi " , "Mumbai" ,"Chandigarh"];

// copy to index 0 the element at index 3
console.log(cities.copyWithin(0, 3, 4));

image.png

10.fill():-

The fill() method is used to change all the elements in an array from startIndex to endIndex.

const cities =  ["Jaipur" , "Delhi " , "Mumbai" ,"Chandigarh"];
console.log(cities.fill("Punjab", 2, 4));
console.log(cities.fill(5, 1));
console.log(cities.fill(6));

image.png

11.filter():-

The filter() method creates a copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.

const numbers=[12,34,98,76,22,121];
const result =numbers.filter((num)=>num>36
);
console.log(result);

image.png

12. toString():-

The toString() method returns a string representing the specified array and its elements.

const array1 = [1, 2, 'a', '1a'];

console.log(array1.toString());

image.png

13. reverse():-

The reverse() method reverses an array.The first index element shifts to last and last element shifts to first changing the order of all the elements.

const cities =  ["Jaipur" , "Delhi " , "Mumbai" ,"Chandigarh"];
console.log(cities.reverse());

image.png

14 .forEach():-

The forEach() method executes a provided function once for each array element.


const cities =  ["Jaipur" , "Delhi " , "Mumbai" ,"Chandigarh"];
cities.forEach(element => 
console.log(element));

image.png

This is all about methods in an array .Kindly refer mdn docs for more such methods. If you like my blog please hit the like button.