Movatterモバイル変換


[0]ホーム

URL:


  1. 給開發者的 Web 技術文件
  2. JavaScript
  3. JavaScript 參考文件
  4. 標準內建物件
  5. Array
  6. Array.prototype.reverse()

此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。

View in EnglishAlways switch to English

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 ⁨2015年7月⁩.

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® 2026 Language Specification
# sec-array.prototype.reverse

瀏覽器相容性

參見

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp