What is best practice for switching two items place in an Array via JavaScript?
For example if we have this array ['one', 'two', 'three', 'four', 'five', 'six']
and want to replace two with five to get this final result: ['one', 'five', 'three', 'four', 'two', 'six']
.
The ideal solution is short and efficient.
Update:
The var temp
trick is working and I know it. My question is: is using JavaScript native Array methods faster or not?
Something like array.splice().concat().blah().blah()
seems is faster and use less memory. I didn't develope a function that using Array methods but I'm sure it's possible.