How to empty an Array in JavaScript?

vishalneeraj-24503
Posted by vishalneeraj-24503 under JavaScript category on | Points: 40 | Views : 630
It's very simply but hard to empty an Array using Javascript.

There are 4 ways to achieve this task.
For Example:-

We make a function:-

function empty_array()
{
//1st way
var array_val1 = [1,2,3,4];
array_val1 = [];

//2nd way
var array_val2 = [1,2,3,4];
array_val2.splice(0,array_val2.length);

//3rd way
var array_val3 = [1,2,3,4];
array_val3.length = 0;

//4th way
var array_val4 = [1,2,3,4];
array_val4 = new Array();

alert(array_val1);
alert(array_val2);
alert(array_val3);
alert(array_val4);
}


Explaination:-
1). array_val1 = [] -> Meaning,we are creating new object or array.
2). array_val3.length = 0 -> Assigning array'length to 0.
3). array_val4 = new Array(); -> Same as 1 option.

Comments or Responses

Login to post response