Useful JavaScript's methods in LWC and Lightning Component - Part I

Both LWC and Aura Components contain significant amount of JavaScript. Aura Component supports ES5 syntax and ES6 promises whereas LWC supports ES6 modules.


if you know how to process list or array in javascript, then calling server side method (apex methods) for sorting and processing is not required. The lightning component will also become fast. In this blog, I am gonna list some useful array methods with example.


1. Push and pop - push is used to add data and pop is used to remove the last data in array.

push method returns the total size of array, and pop returns the removed element.


let fruits = ["Banana", "Orange"];
let size = fruits.push("Apple"); // return 3 to size
console.log(fruits); // print Banana, Orange, Apple
let element = fruits.pop(); // returns 'Apple' to element
console.log(fruits); //print Banana, Orange


2. shift - while pop remove element from last, shift remove element from beginning. This method returns the shifted element.


let fruits = ["Banana", "Orange"];
let element = fruits.shift();
console.log(fruits); // print "Orange"
console.log(element) // print Banan