Answers for "javascript array how to switch position for the first and last element"

0

javascript move last array element to first

let arr1 = [1, 2, 3, 4, 5]
let arr2 = [1, 2, 3, 4 ,5]
// first to the last
arr1.push(arr1.shift()) // [2, 3, 4, 5, 1]

// last to the first
arr2.unshift(arr2.pop()) // [5, 1, 2, 3, 4]
Posted by: Guest on June-10-2021
-2

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]
Posted by: Guest on February-21-2022

Code answers related to "javascript array how to switch position for the first and last element"

Code answers related to "Javascript"

Browse Popular Code Answers by Language