Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

swap first and last element in array javascript

const array = [1, 2, 3, 4, 5];

const tmp = array.pop(); //[1, 2, 3, 4]
array.push(array[0]); //[1, 2, 3, 4, 1]
array[0] = tmp; //[5, 2, 3, 4, 1]
 
PREVIOUS NEXT
Tagged: #swap #element #array #javascript
ADD COMMENT
Topic
Name
6+6 =