JavaScript

June 6, 2023

How to use JavaScript Array reverse()

Program to use JavaScript Array reverse() method


let a = [1,2,3,4,5];
let b = a.reverse();
console.log(b);

Output


[ 5, 4, 3, 2, 1 ]

Steps Description

  1. Declare two variable a and b
  2. Initialize two variable with value of array a = [1,2,3,4,5] and b = a.reverse()
  3. The array reverse() method returns the given array in reverse order of the array elements.
  4. The value of b variable is displayed with the help of console.log inbuilt function

Program to use JavaScript Array reverse() method


let c = ['Cat', 'Dog', 'Tiger', 'Lion', 'Elephant'];
let d = c.reverse();
console.log(d);

Output


[ 'Elephant', 'Lion', 'Tiger', 'Dog', 'Cat' ]

Steps Description

  1. Declare two variable c and d
  2. Initialize two variable with value of array c = ['Cat', 'Dog', 'Tiger', 'Lion', 'Elephant'] and d = c.reverse()
  3. The array reverse() method returns the given array in reverse order of the array elements.
  4. The value of d variable is displayed with the help of console.log inbuilt function

by : Suhel Akhtar

Quick Summary:

Javascript array reverse() method