DEV Community

sutharrahul
sutharrahul

Posted on

Array Method

Array is a collection of similar data elements stored at contiguous memory locations. It is the simplest data structure where each data element can be accessed directly by only using its index number.
In array index number start with 0.
Array data [hello, array, java, c++, 1, 56, 23]
Array index [ 0 , 1 , 2 , 3 , 4, 5 ,6 ]

Array Method

Array Length
array length use for get total number of array. How many elements stored in array

let name = ['hello', 'world', 'Asia', 'india', 'america']
console.log(name.length)

outpur: 5
Enter fullscreen mode Exit fullscreen mode

Total number of element is 5

Push
Insert the new value inside array but value/element will stored in last.

let namess = [12, 45, 76, 24, 46]
namess.push('VYOM','rah');
console.log(namess);

output: [12, 45, 76, 24, 46, 'VYOM','rah']
Enter fullscreen mode Exit fullscreen mode

Slice
The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end ( end not included) where start and end represent the index of items in that array.
Syntax
slice()
slice(start)
slice(start, end)

let namess = [12, 45, 76, 24, 46, 23, 86, 96]
console.log(namess.slice(1,7);

output: [ 45, 76, 24, 46, 23, 86 ]
Enter fullscreen mode Exit fullscreen mode

Splice
The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements
Syntax
splice(start)
splice(start, deleteCount)
splice(start, deleteCount, item1)
splice(start, deleteCount, item1, item2, itemN)

let fruit = ['1', '2', '3', '4', 5, 6, 7, 8];
fruit.splice(2, 1, 'Apple', 'Banana');
console.log(fruit);

output: [ '1', '2', 'Apple', 'Banana', '4', 5, 6, 7, 8 ]
Enter fullscreen mode Exit fullscreen mode

Concatenation
The Concatenationmethod add two array.

let arr1 = [1, 2, 3, 4];
let arr2 = [5, 6, 7];
let arr3 = [8, 9, 10, 11, 12]; 
console.log(arr1.concat(arr2, arr3));

output: [1, 2, 3, 4, 5 , 6, 7, 8, 9, 10, 11, 12]
Enter fullscreen mode Exit fullscreen mode

Include
The include method returns "true or false" if the value stored in correct index output will "true" else output will be false.

let num = [1, 2, 3, 4, 5, 6, 7, 8];
console.log(num.includes(7, 6));

output: True

let num = [1, 2, 3, 4, 5, 6, 7, 8];
console.log(num.includes(7, 8));

output: false
Enter fullscreen mode Exit fullscreen mode

7 is a value and 6 is index no. so 7 is stored in index of 6 output is true
if (7,8) it's mean 7 is stored index of 8 which is false because 7 is stored in index of 6

Indexof
Indexof method use to get index number of value means in which index no. value is stored.

let num = [1, 2, 3, 'Anurag', 4, 5, 6, 7, 8];
console.log(num.indexOf('Anurag'));

output: 3
Enter fullscreen mode Exit fullscreen mode

if the same value stored multiple times in same array in that case output will only one index number which come first.

Is Array
isarray Method use is this array or not if it is than output will true is not output will false

let num = [1, 2, 3, 'Anurag', 4, 5, 6, 7, 8, 'Anurag', 'Anurag'];
console.log(Array.isArray(num));

output: True

let num1= dreamworld
console.log(Array.isArray(num1));

output: false
Enter fullscreen mode Exit fullscreen mode

Join
The join method use when we want to add a value in array with all the value of array.

let Arr1 = [1, 2, 3, 4, 5, 6, 7];
let var1 = Arr1.join(' a '); //a will join all value of array
console.log(var1);

output:1 a 2 a 3 a 4 a 5 a 6 a 7 

Enter fullscreen mode Exit fullscreen mode

lots Array method are available.

Top comments (0)