How to empty an array in JavaScript?

JavaScript arrays are used to store multiple values in a single variable.An array is a special variable, which can hold more than one value at a time.

Here we have an array
Eg:
var Arr = [1,2,3,4,5];
4 ways to empty this array

Method 1:

Arr = [];

Method 2:

Arr.length = 0;

Method 3:

Arr.splice(0,Arr.length)

Method 4:

while(Arr.length > 0)
    Arr.pop();
}

Clearing object array

var website = {url:"http://www.webspeckle.com", type:"blog"};
console.log(website); //{ url:"http://www.webspeckle.com", type:"blog"}
var websiteData = null;
for(websiteData in website)
{
    website[websiteData] = null;
}
console.log(website); //{ url:null, type:null}

No comments:

Post a Comment