Using the built-in Array.prototype.reverse()
method in JavaScript is a straightforward and clean way to reverse an array.
s.reverse()
: This method reverses the array in place, meaning the original array s is modified directly without using additional space for another array.
-
Time complexity:
$O(n)$ , where$n$ is the length of the array. Thereverse
method iterates through the array and swaps elements, resulting in linear time complexity. -
Space complexity:
$O(1)$ , as thereverse
method operates in place and does not use additional space proportional to the size of the input.