DEV Community

Kiruthiga S
Kiruthiga S

Posted on Edited on

Array Methods

1.Array Length
The length property returns the length (size) of an array
The length property can also be used to set the length of an array

let a = ["HTML", "CSS", "JS", "React"];
// console.log(a.length);
a.length=2;
console.log(a);
Enter fullscreen mode Exit fullscreen mode

Output:4
(2) ['HTML', 'CSS']

2.toString()
Converts the given value into the string with each element separated by commas

let b  = ["HTML", "CSS", "JS", "React"];
// let c = b.toString();
// console.log(c);
console.log(b.toString());
Enter fullscreen mode Exit fullscreen mode

Output:HTML,CSS,JS,React

3.at()
ES2022 introduced the array method at()
Returns an indexed element from an array

let d  = ["HTML", "CSS", "JS", "React"]; 
//    let e = d.at(2);
//    console.log(e);
console.log(d.at(3));
Enter fullscreen mode Exit fullscreen mode

Output:React

4.join()
The join() method also joins all array elements into a string.
It behaves just like toString(), but in addition you can specify the separator

let f = ["HTML", "CSS", "JS", "React"];
console.log(f.join('|'));
Enter fullscreen mode Exit fullscreen mode

Output:HTML|CSS|JS|React

5.pop()
Used to remove elements from the end of an array

 let g = ["HTML", "CSS", "JS", "React"];
 g.pop();
 console.log(g);
Enter fullscreen mode Exit fullscreen mode

Output: ['HTML', 'CSS', 'JS']

6.push()
adds a new element to an array (at the end)

let h = ["HTML", "CSS", "JS", "React"];
h.push("java");
console.log(h);
Enter fullscreen mode Exit fullscreen mode

Output: ['HTML', 'CSS', 'JS', 'React', 'java']

7.shift()
Removes the first array element and "shifts" all other elements to a lower index

let i = ["HTML", "CSS", "JS", "React"];
i.shift();
console.log(i);
Enter fullscreen mode Exit fullscreen mode

Output: ['CSS', 'JS', 'React']

8.unshift()
adds a new element to an array (at the beginning), and "unshifts" older elements

let j = ["HTML", "CSS", "JS", "React"];
j.unshift("java");
console.log(j);
Enter fullscreen mode Exit fullscreen mode

Output: ['java', 'HTML', 'CSS', 'JS', 'React']

9.isArray()
ECMAScript 5 (JavaScript 2009) added the new method Array.isArray() to JavaScript
isArray() is a built-in method of the Array object. It checks whether a value is an array

let k = ["HTML", "CSS", "JS", "React"];
console.log(Array.isArray(k));
Enter fullscreen mode Exit fullscreen mode

Output:true

10.delete()
Used to delete the element in the array

let l = ["HTML", "CSS", "JS", "React"];
delete l[0];
console.log(l);
// console.log(l[0]);
Enter fullscreen mode Exit fullscreen mode

Output:[empty, 'CSS', 'JS', 'React']

11.concat()
Used to concatenate two or more arrays and it gives the merged array
This does not change the existing arrays. It always returns a new array

let m = ["HTML", "CSS", "JS", "React"];
let n=["java","python"];
let o=m.concat(n);
console.log(o);
Enter fullscreen mode Exit fullscreen mode

Output:['HTML', 'CSS', 'JS', 'React', 'java', 'python']

12.copyWithin
copies array elements to another position in an array

let p = ["HTML", "CSS", "JS", "React"];
p.copyWithin(2,0);
console.log(p);
Enter fullscreen mode Exit fullscreen mode

Output: ['HTML', 'CSS', 'HTML', 'CSS']

13.flat()
Used to flatten the array i.e. it merges all the given array and reduces all the nesting present in it

let q = ["HTML", "CSS", "JS",[1,2], "React",['5','6']];
let r=q.flat();
console.log(r);
Enter fullscreen mode Exit fullscreen mode

Output:['HTML', 'CSS', 'JS', 1, 2, 'React', '5', '6']

14.splice()
used to Insert and Remove elements in between the Array

let s = ["HTML", "CSS", "JS", "React"];
// s.splice(2 ,2);
// s.splice(1,0,"python");
s.splice(2,0,"java")
console.log(s);
Enter fullscreen mode Exit fullscreen mode

Output:['HTML', 'CSS', 'java', 'JS', 'React']

15.slice()
Returns a new array containing a portion of the original array, based on the start and end index provided as arguments
Does not change the original array

let t = ["HTML", "CSS", "JS", "React","java","python"];
// let u=t.slice();
let u=t.slice(1,4);
console.log(u);
// console.log(t);
Enter fullscreen mode Exit fullscreen mode

Output:['CSS', 'JS', 'React']

Top comments (0)