Posted on

Reverse a string in Javascript

Reverse a string - Javascript:

//function to reverse a string
function reverseString(str) {
  // split the string into an array of characters
  var splitString = str.split("");
  // reverse the array of characters
  var reverseArray = splitString.reverse();
  // join the array of characters into a string
  var joinArray = reverseArray.join("");
  // return the reversed string
  return joinArray;

const result = reverseString("hello");

console.log(result); //olleh

Enter fullscreen mode Exit fullscreen mode

Top comments (0)