Array.prototype.reverse()
reverse()
方法會原地(in place)反轉(reverses)一個陣列。陣列中的第一個元素變為最後一個,而最後一個元素則變成第一個。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
語法
a.reverse()
回傳值
反轉後的陣列。
描述
reverse
方法將原地(in place)變換(transposes)呼叫此方法的陣列物件之元素至其顛倒的位置,改變原陣列後,並回傳此陣列之參考位址(reference)。
範例
反轉陣列中之元素
下列範例建立了一個包含三個元素的陣列 a
,接著反轉此陣列。呼叫 reverse()
會回傳一個反轉後的原陣列 a
之參考。
var a = ['one', 'two', 'three'];
var reversed = a.reverse();
console.log(a); // ['three', 'two', 'one']
console.log(reversed); // ['three', 'two', 'one']
規範
規格 | 狀態 | 註解 |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.1. |
ECMAScript 5.1 (ECMA-262) The definition of 'Array.prototype.reverse' in that specification. |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Array.prototype.reverse' in that specification. |
Standard | |
ECMAScript (ECMA-262) The definition of 'Array.prototype.reverse' in that specification. |
Living Standard |
瀏覽器相容性
BCD tables only load in the browser