How to reverse an array in Javascript?

 

Method 1

<script>

    let numbers_array = [11, 22, 33, 54, 55];
     
    console.log("Original Array: ");
    console.log(numbers_array);
     
    numbers_array.reverse();
     
    console.log("Reversed Array: ");
    console.log(numbers_array);
</script>

    Output:

Original Array: 
[ 11, 22, 33, 44, 55 ]
Reversed Array: 
[ 55, 44, 33, 22, 11 ]
Method 2
 let original_array = [11, 22, 33, 44];
 
 let reversed_array = [];
 
 console.log("Original Array: ");
 console.log(original_array);
 
  for (let i = original_array.length - 1; i >= 0; i--) {
        reversed_array.push(original_array[i]);
  }
 
  console.log("Reversed Array: ");
  console.log(reversed_array);

    Output:

Original Array: 
[ 11, 22, 33, 44, 55 ]
Reversed Array: 
[ 55, 44, 33, 22, 11 ]
Method 3
let original_array = [11, 22, 33, 44, 55, 66];
 
let reversed_array = [];
 
console.log("Original Array: ");
console.log(original_array);
 
original_array.forEach((element) => {
      reversed_array.unshift(element);
});
 
console.log("Reversed Array: ");
console.log(reversed_array);
Original Array: 
[ 11, 22, 33, 44, 55,66 ]
Reversed Array: 
[66, 55, 44, 33, 22, 11 ]

CPU vs GPU Architecture

  CPU vs GPU Architecture CPU (Central Processing Unit) and GPU (Graphics Processing Unit) have distinct architectural differences, optimize...

Best for you