Cara menggunakan javascript array best practices

The reverse() method reverses an array in place and returns the reference to the same array, the first array element now becoming the last, and the last array element becoming the first. In other words, elements order in the array will be turned towards the direction opposite to that previously stated.

Try it

Syntax

Return value

The reference to the original array, now reversed. Note that the array is reversed in place, and no copy is made.

Description

The reverse() method transposes the elements of the calling array object in place, mutating the array, and returning a reference to the array.

The reverse() method preserves empty slots. If the source array is sparse, the empty slots' corresponding new indices are deleted and also become empty slots.

reverse is intentionally generic; this method can be called on objects resembling arrays. Objects which do not contain a length property reflecting the last in a series of consecutive, zero-based numerical properties may not behave in any meaningful manner.

Examples

Reversing the elements in an array

The following example creates an array items, containing three elements, then reverses the array. The call to reverse() returns a reference to the reversed array items.

const items = [1, 2, 3];
console.log(items); // [1, 2, 3]

items.reverse();
console.log(items); // [3, 2, 1]

Reversing the elements in an array-like object

The following example creates an array-like object obj, containing three elements and a length property, then reverses the array-like object. The call to reverse() returns a reference to the reversed array-like object obj.

const obj = {0: 1, 1: 2, 2: 3, length: 3};
console.log(obj); // {0: 1, 1: 2, 2: 3, length: 3}

Array.prototype.reverse.call(obj); //same syntax for using apply()
console.log(obj); // {0: 3, 1: 2, 2: 1, length: 3}

The reverse() method returns the reference to the same array

The reverse() method returns reference to the original array, so mutating the returned array will mutate the original array as well.

const numbers = [3, 2, 4, 1, 5];
const reversed = numbers.reverse();
// numbers and reversed are both in reversed order [5, 1, 4, 2, 3]
reversed[0] = 5;
console.log(numbers[0]); // 5

In case you want reverse() to not mutate the original array, but return a shallow-copied array like other array methods (e.g. map()) do, you can do a shallow copy before calling reverse(), using the spread syntax or Array.from().

const numbers = [3, 2, 4, 1, 5];
// [...numbers] creates a shallow copy, so reverse() does not mutate the original
const reverted = [...numbers].reverse();
reverted[0] = 5;
console.log(numbers[0]); // 3

Using reverse() on sparse arrays

Sparse arrays remain sparse after calling reverse(). Empty slots are copied over to their respective new indices as empty slots.

console.log([1, , 3].reverse()); // [3, empty, 1]
console.log([1, , 3, 4].reverse()); // [4, 3, empty, 1]

Specifications

Specification
ECMAScript Language Specification
# sec-array.prototype.reverse

Browser compatibility

BCD tables only load in the browser

See also