운전을 해 본 사람이라면 기어 변속기 옆에 R이라는 영어를 본 적이 있을 것이다.
후진할 때 기어를 R에 놓고 하는데 reverse의 약자로 후진, 즉 뒤집다 라는 뜻을 가지고 있다.
오늘 배워볼 메서드는 reverse() 이다.
reverse()는 배열의 순서를 반전시킨다.
그러면 첫 번째 요소는 마지막 요소가 되며 마지막 요소는 첫 번째 요소가 된다.
a.reverse()
문법도 아주 간편하다.
예시
const array1 = ['one', 'two', 'three'];
console.log('array1:', array1);
// Expected output: "array1:" Array ["one", "two", "three"]
const reversed = array1.reverse();
console.log('reversed:', reversed);
// Expected output: "reversed:" Array ["three", "two", "one"]
// Careful: reverse is destructive -- it changes the original array.
console.log('array1:', array1);
// Expected output: "array1:" Array ["three", "two", "one"]