-6

I have an array as:

var arr = [1,2,3,4]

//Push and element to array
arr.push(5)

//Now, arr = [1,2,3,4,5]

I need to display my array as

Elements in array arr is:
5,1,2,3,4

Arr.reverse() gives me 5,4,3,2,1. But i need 5,1,2,3,4

2 Answers2

1

Simply use Array.prototype.reverse():

console.log(arr.reverse());

References:

David Thomas
  • 249,100
  • 51
  • 377
  • 410
0

This is the code you need to use :

arr.reverse();

Here is the w3school : http://www.w3schools.com/jsref/jsref_reverse.asp

mp9007
  • 79
  • 2
  • 11
  • I think you didn't understand the question. arr.reverse() gives me [5,4,3,2,1]. I need [5, 1,2,3,4] and for generalised condition. – Suraj Acharya Oct 03 '16 at 16:37