How to use JavaScript Array reverse()

Array reverse

Array reverse() method

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

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

Array reverse() method

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

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