Posts

Showing posts from February, 2022

Ways to empty JavaScript array

There are different ways to empty an array in Javascript. By assigning an empty array var numbers =[1,3,5,6]; numbers =[]; By assigning array length to 0. var letters = ["a", "b", "c"]; letters.length = 0; By poping the elements of the array. var randomIntegers =[1,4,5,6]; while(randomIntegers.length > 0) { randomIntegers.pop(); } By using .splice() var cities =["Mumbai","Pune"]; cities.splice(0, cities.length)

Clone object in JavaScript

Object.assign() is a method which is used for cloning an object in JavaScript. Here is an example var person = { talk: "true" }; var male = Object.assign({}, person);