Answers for "how to swap two elements in an array javascript"

3

how to swap two elements in an array js

// Since ES6
[a[0], a[1]] = [a[1], a[0]]
Posted by: Guest on December-01-2020
1

how to swap two elements in an array javascript

// Before ES6
const temp = a[x]; // NOT RECOMMENDED UNLESS COMPLETELY UNAVOIDABLE
a[x] = a[y];
a[y] = temp;
Posted by: Guest on December-01-2020

Code answers related to "how to swap two elements in an array javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language