Asked 8 years ago
28 Nov 2016
Views 1398
jassy

jassy posted

how to remove first and last element from array in JavaScript

suppose

var device = array("iphone 5","iphone 6","iphone 7","iphone 8");


now how can i remove first and last element from array by JavaScript
is there any method or function in JavaScript so i can remove first and last element by it ?


ravi

ravi
answered Nov 30 '-1 00:00

i use like below code. when i need to remove first and last element from array


var device = array("iphone 5","iphone 6","iphone 7","iphone 8");
var device_revise;
for(i=1;i<device.length-1;i++){
device_revise[]=device[i];
}

Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00


var device = array("iphone 5","iphone 6","iphone 7");
device.shift();
device.pop();


shift() remove first from array
pop() remove last from array

shyam

shyam
answered Nov 30 '-1 00:00


device.splice(0,1);//remove first
device.splice(device.length-1,1);remove last


Array.prototype.splice(index,n)
splice is remove element from index , n number of element will be removed
Mahesh Radadiya

Mahesh Radadiya
answered Nov 30 '-1 00:00

Array.prototype.slice(from,to) is extract the element from index --> to index and return new array with extracted result



device.slice(1,device.length-1)


one line code will remove first and last element from array in javascript
Post Answer