DEV Community

R.Shobika CSE
R.Shobika CSE

Posted on

ARRAY METHODS IN JS

In Array method there are various methods in JavaScript.

  1. Array length:

It return the length of the array and this method is used to set the length of the array also.

const num =[10,5,20,3];
console.log(num.length);
Enter fullscreen mode Exit fullscreen mode

output:
4

2.Array to String():

This method return the array as a string in a commas separated

const num = [10, 20,3,50];
const list= num.toString();
console.log(list);
Enter fullscreen mode Exit fullscreen mode

Output:

10,20,3,50

3.Array at():

This will return the particular index of the element in the array .
The negative index value does not support to the JavaScript like other Programming Language . At that time we can use the array at () method to specify the positive index and we can also give the negative index in the JavaScript .
In Positive index the index value will be start from 0
but in negative index it will start from the last element in the array -1 [-1 represent the last element of the array ].

const num = [10,2,50,3,4];
console.log(num.at(3));
console.log(num.at(-3));
Enter fullscreen mode Exit fullscreen mode

output:
3
50

4.Array Join():

This method is also behave like a array to String() method but in this method we can specify the seperator,
It will also return a String

const num =[10,2,30,4,5];
console.log(num.join("*"));
Enter fullscreen mode Exit fullscreen mode

output:
10*2*30*4*5

5.Array pop():

This method remove the last element in the array.

const num = [10,20,4,5,3];
console.log(num);
console.log(num.pop());
Enter fullscreen mode Exit fullscreen mode

output:
[10,20,4,5]
3

6.Array push():

This method insert the element in the last position in the array.

const num =[10,20,4,5];
console.log(num.push(6));
console.log(num);
Enter fullscreen mode Exit fullscreen mode

output:
6
10,20,4,5,6

ARRAY ITERATION METHOD

1.Array for...of:

This method give the value of the array. Automatically take each value one by one

const num = [10,2,3,4,5];
for(let number of num){
console.log(number);
} 
Enter fullscreen mode Exit fullscreen mode

output:
10
2
3
4
5

2.Array for...in:

This method will give the index of the value.

const num = [10,20,3,4,5];
for(let number in num){
console.log(number);
}
Enter fullscreen mode Exit fullscreen mode

output:
0
1
2
3
4

Top comments (0)