Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

How to swap two array elements in JavaScript

// How to swap two array elements in JavaScript
const arr = ['a', 'b', 'c', 'e', 'd'];
[arr[1], arr[0]] = [arr[0], arr[1]]
console.log(arr);
// Output:
// [ 'b', 'a', 'c', 'e', 'd' ]
 
PREVIOUS NEXT
Tagged: #How #swap #array #elements #JavaScript
ADD COMMENT
Topic
Name
6+2 =