Asked 30+ days ago
28 Nov 2016
Views 285
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 Jan 21 '18 20:16

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];
}

Edit Answer
Share
Mitul Dabhi

Mitul Dabhi
answered Jan 21 '18 20:16


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


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

Edit Answer
Share
shyam

shyam
answered Jan 21 '18 20:16


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
Edit Answer
Share
Mahesh Radadiya

Mahesh Radadiya
answered Jan 21 '18 20:16

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
Edit Answer
Share
Post Answer