-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse.js
More file actions
44 lines (36 loc) · 1.57 KB
/
Copy pathreverse.js
File metadata and controls
44 lines (36 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// reverse()
// What
// - The reverse() method in JavaScript is used to reverse the order of elements in an array.
// Why
// - The reverse() method is useful when you need to manipulate or process an array by reversing its order.
// - It is commonly used in various programming scenarios, such as sorting data in descending order, reversing a sequence of elements, or creating user-friendly interfaces.
// How
// - The reverse() method modifies the original array in-place and does not create a new array.
// - It reverses the order of elements in the array by swapping the first element with the last element, the second element with the second-to-last element, and so on.
// Syntax
// array.reverse()
// Example
// Reversing an array of numbers
const numbers = [1, 2, 3, 4, 5];
numbers.reverse();
console.log(numbers); // Output: [5, 4, 3, 2, 1]
// Reversing an array of strings
const fruits = ["apple", "banana", "orange"];
fruits.reverse();
console.log(fruits); // Output: ["orange", "banana", "apple"]
// Reversing an array of objects
const people = [
{ name: "John", age: 25 },
{ name: "Jane", age: 30 },
{ name: "Bob", age: 35 },
];
people.reverse();
console.log(people);
// Output: [
// { name: "Bob", age: 35 },
// { name: "Jane", age: 30 },
// { name: "John", age: 25 },
// ]
// Note
// - The reverse() method modifies the original array in-place. If you need to preserve the original order of elements, you should create a copy of the array before calling the reverse() method.
// - The reverse() method does not return a new array. Instead, it modifies the existing array.