reverse Mutates While toReversed Copies

reverse flips the array in place and returns the same reference, while toReversed returns a distinct reversed copy and leaves the source untouched.

JavascriptData Integrity

Code

javascriptsnippet
16 lines
const queue = ['a', 'b', 'c', 'd'];

// reverse 原地翻转:返回值就是同一个数组
const same = queue.reverse();
console.log('reversed   :', queue.join(','));
console.log('same object:', same === queue);

// toReversed 拷贝翻转:原数组保持原序
const source = ['a', 'b', 'c', 'd'];
const flipped = source.toReversed();
console.log('flipped    :', flipped.join(','));
console.log('untouched  :', source.join(','));
console.log('distinct   :', flipped !== source);

// toReversed 之后再 reverse 回去,元素逐项相等
console.log('roundtrip  :', flipped.reverse().join(',') === source.join(','));

Output

Captured from a sandboxed run of this snippet in the build pipeline.

reversed   : d,c,b,a
same object: true
flipped    : d,c,b,a
untouched  : a,b,c,d
distinct   : true
roundtrip  : true

What the code does

The demo data lives in module-level bindings: queue, same, source, flipped.

Applying it

The demo output is labeled (reversed :, same object:, flipped :), so every printed line maps back to its originating operation.

Statements that matter

const source = ['a', 'b', 'c', 'd'];
const flipped = source.toReversed();
const queue = ['a', 'b', 'c', 'd'];

Interactive Tools for This Topic

FAQ

Can I paste this into a project as-is?
Yes. Paste it in and run; no package manager step is needed. Its state lives in module-level bindings: `queue`, `same`, `source`.