switching two items in an array - Javascript Array Operation

Javascript examples for Array Operation:Array Element

Description

switching two items in an array

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=( function() {/*  ww  w . ja  v  a2 s .  c  o  m*/
var my_array = [1, 2, 3, 4, 5, 6];
var swapArrayPosition = function (array, index_1, index_2) {
  var temp = array[index_1];
  array[index_1] = array[index_2];
  array[index_2] = temp;
}
swapArrayPosition(my_array, 4, 1);
console.log(my_array);
    });

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials