In-Depth Article about Array in Javascript.

In-Depth Article about Array in Javascript.

The Array is a single variable that is used to store different elements. it is often used when we want to store a list of elements and access them by a single variable. array in javascript can be defined and initialized in two ways, array literal and array constructor,

Array Literal

Array literal syntax is simple. it takes a list of values separated by a comma and closed in square brackets.

Syntax

var array-name = ["element-1", "element-2", "element-3", "element-4"]

Array Constructor

You can initialize an array with array constructor syntax using a new keyword. The array constructor has the following three forms.

Syntax

var array-name = new Array()

var array-name = new Array(num.lenght)

var array-name = new array ("element-1", "element-2")

Please note that an array can only have numeric index (key) index and cannot be of string or any other data type.

Javascript Array Methods

1. Array.Push( )

Adding an element at the end of an array, as arrays in javascript are multiple objects, we can easily add or remove elements from the array. we can modify the array.

Syntax

array.push("element name")

var array1 = ["India", "USA", "Japan"];
array2=array1.push("Uk");
console.log(array2);

// India USA Japan UK

2. Array.pop( )

Removing an element from the end of an array.

Syntax

array.pop( )

var arr1 = [10, 20, 30, 40, 50, 60];
arr1.pop();
console.log(arr1);

// 10 20 30 40 50

3. Array.unshift

Adding elements at the front of an array, and returns the new length of the way.

Syntax

unshift.("New value" );

var arr1 = [4, 5, 6,];

arr1.unshift(1, 2, 3);
console.log(arr1);

// 1 2 3 4 5 6

4. Array.shift( )

The shift method Removes elements at the beginning of an array.

Syntax

array-name.shift

var arr1 = ["West Bengal", "Mumbai", "Delhi", "Tamilnadu", "Pune"];
arr1.shift();
console.log(arr1);

// Mumbai Delhi Tamilnadu Pune

5. Array.slice( )

The slice method returns selected elements in an array. the slice method selects from a given start, up to given end. the slice method does not change the original array.

Syntax

array.slice(start index, endindex)

var arr1 = ["India", "USA", "Japan", "China", "Bangladesh", "Sri Lanka"];

console.log(arr1.slice(2,  4));

// Japan China

6. Array.splice( )

The Splice method changes the contents of the array by adding or removing elements.

Syntax

Array.splice(index, "new value");

var arr1 = ["Banana", "Orange", "Apple", "Mango"];

arr1.splice(2,0, "lemon");

console.log(arr1);

// 'Banana', 'Orange', 'lemon', 'Apple', 'Mango'

7. to.string( )

The to.string method returns a string with array values separated by commas.

Syntax

array-name.tostring( )

var arr1 = [1, 2, 3, 4, 5, 6, 7, 8]

console.log(arr1.toString());

// 1, 2, 3, 4, 5, 6, 7, 8

7. Concat( )

The contact ( ) method concatenates (joins) two or more arrays.

array3.concat(array-1, array-2)

var arr1 = ["a", "b", "c"];
var arr2 = ["e", "f", "g"]

var arr3 = arr1.concat(arr2);

console.log(arr3);

// 'a', 'b', 'c', 'e', 'f', 'g'

8. Indexof

The javascript array indexof ( ) method is used to search the position of a particular element in a given array.

Syntax

array-name.index("element-name");

var arr1 = ["a", "b", "c", "d", "e"];

console.log(arr1.indexOf("d"));

// 3

9. Array.isArray

The array.isArray() method determines whether the value passed to this function is an array or not. This function returns true if the argument passed is an array else it returns false.

Array.isArray(obj)

var arr1 = ["a", "b", "c", "d", "e"];

var arr2 = Array.isArray(arr1);

console.log(arr2);

// true

Length

The Length property sets or returns the number of elements in an array.

array-name.length

var arr1 = ["a", "b", "c", "d", "e"];

console.log(arr1.length);

// 5

11. Includes

The includes ( ) method returns true. if an array contains a specified value. the includes method returns false if the value is not found.

Syntax

array.includes(search element);

var arr1 = ["a", "b", "c", "d", "e"];

console.log(arr1.includes("c"));

// true

console.log(arr1.includes(3));

// false

Conclusion

I hope this article helps you to understand Array in Javascript. if you have any suggestions about this topic you can comment below. and don't forget to like and share.