Javascript (JS) useful functions

Get unique values from an array

let newArr = [...new Set([1, 2, 3, 3])]
// => [1,2,3]

Remove falsy values from array

let newArr = [0, undefined, null, false, true, 'myval'].filter(Boolean)
// => [true, 'myval]

Merge objects

let location = { language: 'English', country: 'Singapore' }
let personalInfo = { name: 'LV', age: 30 } 
let merged = {...location, ...personalInfo }
// =>{"language":"English","country":"Singapore","name":"LV","age":30}

Three dots
Common usage:

const names = ["Hai", "Son", "Linh"]
const [firstName, ...restNames] = names;

console.log(firstName); // Hai
console.log(restNames); // ["Son", "Linh"]
const info = {
  name: "LV",
  age: "21",
  gender: "male"
}
const { name, ...restThings } = info;
// console.log(name) => "LV"
// console.log(restThings) => {"age":"21","gender":"male"}
const names = ["LV", "Jack"];
const newArr = ["Antony", ...names]; //["Antony", "LV", "Jack"]
const blog = {
  blog1: "need_replaced"
  blog2: "medium",
  blog3: "reddit"
}

const extended = {
  ...blog,
  blog1: "anonystick",
};

// { blog1: "anonystick", blog2: "medium", blog3: "reddit" } 

Get arguments

function myFunc(name, ...rest) {
  console.log(name); // 'LV'
  console.log(rest); // ["Kevin", "Jack"]
}

myFunc("LV", "Kevin", "Jack");

Create date time object

const dateFields = [1970, 0, 1];  // 1 Jan 1970
const d = new Date(...dateFields);