Which method is used to Clear an array using JavaSctipt?

 Posted by Syedshakeer on 11/30/2009 | Category: JavaScript Interview questions | Views: 12965
Select from following answers:
  1. Clear()
  2. Remove()
  3. ClearAll()
  4. Delete()
  5. All Above

Show Correct Answer


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Chinmayeeg on: 9/7/2012 | Points: 10
There is no clear() method available in javascript. You can use following methods to clear arrays

Method 1:
var a =  [1,2,3,4];

a = [];
// empty array

Method 2:
var a =  new Array(1,2,3,4);

a = new Array();
// reinitialize array

Method 3:
a.length = 0;


Method 4: // recommended
a.splice(0, a.length);

Login to post response