create a reverse function in javascript

To create a reverse function in JavaScript, you need to first determine whether you want to reverse a string or an array. Here are two examples:

Reversing a String

index.tsx
function reverseString(str) {
  return str.split("").reverse().join("");
}
75 chars
4 lines

This function takes a string str as input, splits it into an array of characters using the split("") method, reverses the order of the characters using the reverse() method, and then joins the characters back together into a new string using the join("") method.

Reversing an Array

index.tsx
function reverseArray(arr) {
  return arr.reverse();
}
55 chars
4 lines

This function takes an array arr as input and simply calls the reverse() method on it, which reverses the order of the elements in the array in place (i.e., it modifies the original array and does not create a new one).

gistlibby LogSnag