Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how to reverse array in javascript

// reversing an array in javascript is kinda hard. you can't index -1.
// but i can show how you can do it in 4 lines.

var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

for (var i = myArray.length - 1; i > 0; i -= 1) {
	myArray.shift();
	myArray.push(i);
}

console.log(myArray); // output: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
Source by www.askpython.com #
 
PREVIOUS NEXT
Tagged: #reverse #array #javascript
ADD COMMENT
Topic
Name
1+2 =