Array.prototype.reverse()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
reverse()
方法會原地(in place)反轉(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"]
語法
js
a.reverse()
回傳值
反轉後的陣列。
描述
reverse
方法將原地(in place)變換(transposes)呼叫此方法的陣列物件之元素至其顛倒的位置,改變原陣列後,並回傳此陣列之參考位址(reference)。
範例
反轉陣列中之元素
下列範例建立了一個包含三個元素的陣列 a
,接著反轉此陣列。呼叫 reverse()
會回傳一個反轉後的原陣列 a
之參考。
js
var a = ["one", "two", "three"];
var reversed = a.reverse();
console.log(a); // ['three', 'two', 'one']
console.log(reversed); // ['three', 'two', 'one']
規範
Specification |
---|
ECMAScript® 2025 Language Specification # sec-array.prototype.reverse |
瀏覽器相容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
reverse |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.