top 10 useful javascript method

in this article, i will show you top 10 useful javascript method you should at least know.

(1)tostring: the toString() method convert an array to a string , and return the result.

const arr=["A","B","C"];
arr.toString();
//"A,B,C"

(2)slice: the slice() method selects a part of an array, and returns the new array.

const arr=["A","B","C"];
arr.slice(1,2);
//"B"

(3)push: the push() method add new element to the end of array,and return the new length.

const arr=["A","B","C"];
arr.push("D");
//["A","B","C","D"]

(4)includes: the includes() method determines whether an array contains a specified element.

const arr=["A","B","C"];
arr.includes("A");
//True

(5)filter: the filter() method create an array filed with all array elements that pass a condition inside the provided function.

const arr=[1,5,6,8,2,3];
arr.filter(m => m >3);
//[5, 6, 8]

(6)pop: the pop() method removes the last element of an array, and returns that element.

const arr=["A","B","C"];
arr.pop();
//["A","B"]

(7)map: the map() method creates a new array with the result of calling a function for every array element.

const arr=[1,8,6,4];
arr.map(m => m*2);
//[1,16,12,8]

(8)shift: the shift() method removes the first element of an array,and returns that element

const arr=["A","B","C"];
arr.shift();
//["B","C"]

(9)foreach: the foreach() method can help you to loop over array's items.

const arr = [1, 2, 3, 4];
arr.forEach(m => {
  console.log(m); // 1 2 3 4
});

(10)Array.from : the Array.from() method you iterate over all elements and add each of them to an intermediate array , so that you can use other array methods like reduce, map, filter and so on

const name = "webdeveloper";
Array.from(name);
//["w","e","b","d","e","v","e","l","o","p","e","r"]


Post a Comment

0 Comments