Introduction
JavaScript is a lightweight and flexible programming language of the web. It powers the dynamic behavior on most websites. JavaScript is just-in-time compiled programming language. So here are some tips and tricks for JavaScript.
Unique Value Filter on Array
An array is an object that can store multiple values at once. For example,
const array = [1,2,3]
The array is storing 3 values.
Create an Array
Using an array literal
const array1 = ["eat", "sleep"];
Using the new keyword
const array2 = new Array("eat", "sleep");
Now if we want to find unique values then we can use,
const array = [1,1,2,3,6,6,3,1]
const uniArray = [...new Set(array)];
console.log(uniArray);
Output
[1,2,3,6]
Filter Array on condition
let array = ["The","Coding","Master"];
console.log(array.filter(x=>x.length >3));
Output
["Coding", "Master"]
Check item availability in Array
let array = ["The","Coding","Master"];
console.log(array.includes("The"));
Output
true
Truncate Array
let array = [0,1,2,3,4,5];
array.length=3;
console.log(array);
Output
[0,1,2]
Last Item in Array
let a= [0,1,2,3];
console.log(a.slice(-1));
Output
[3]
Join All Item from Array
let arr= ['a','b','c','d'];
console.log(arr.join("-"));
Output
"a-b-c-d"
Converts Array to string
const arr = ['hello','js'];
console.log(arr.toString());
Output
"hello,js"
Concat Array in Array
let arr= ['a','b'];
console.log(arr.concat(['c']));
Output
["a", "b", "c"]
Index Of Item in Array
let arr= ['a','b','c'];
console.log(arr.indexOf('c'));
Output
2
Last Index Of Item in Array
let arr= ['a','b','a'];
console.log(arr.lastIndexOf('a'));
Output
2
Manipulate Item in Array
let arr= [1,2,3];
console.log(arr.map(x=>x*2));
Output
[2, 4, 6]
Reverse Array
let arr= [1,2,3];
console.log(arr.reverse());
Output
[3, 2, 1]
Sort Array
let arr= [22,15,13];
console.log(arr.sort());
Output
[13, 15, 22]
Remove last Item in Array
let arr= [22,15,13];
console.log(arr.pop());
Output
13
Push Item in Array
let arr= [22,15,13];
arr.push(8);
console.log(arr);
Output
[22, 15, 13, 8]
Push Item in Array
let arr= [22,15,13];
arr.push(8);
console.log(arr);
Output
[22, 15, 13, 8]
Remove first Item in Array
let arr= [2,1,3];
arr.shift();
console.log(arr);
Output
[1,3]
Add new Item in Array to beginning

Join the conversation! Your thoughts help the community grow.