Empty an Array

by Krishna Ananthi

JavaScript

//1
var ar=[1,2,3,4,5,6,7];
ar.length=3;
console.log(ar);
//2
var ar=[1,2,3,4,5,6,7];
delete ar[3]; // index will be undefined
console.log(ar);
//3 
ar.pop(); 
console.log(ar);
//4
ar.shift(); 
console.log(ar);
//5
var ar=[1,2,3,4,5,6,7];
ar.slice(3,5);
console.log('slice 3 to 5',ar.slice(3,5));
console.log(ar);

//6
var ar=[1,2,3,4,5,6,7];
console.log('splice 3 to 5',ar.splice(3,5));
console.log('splice 3',ar.splice(3));
console.log('slice 3',ar.slice(3));
console.log(ar);

/*
https://stackoverflow.com/questions/37601282/javascript-array-splice-vs-slice

The splice() method returns the removed item(s) in an array and slice() method returns the selected element(s) in an array, as a new array object.

The splice() method changes the original array and slice() method doesn’t change the original array.

The splice() method can take n number of arguments and slice() method takes 2 arguments.

*/