3 Ways to Reverse a String using JavaScript

3 JavaScript Code Examples for Reversing a String

function reverseString1(str) {    
  //This way uses an array to reverse the string
  var strArr = str.split("");
  var reverseStringArray = strArr.reverse();
  var reversedString = reverseStringArray.join("");
  return reversedString;
}

reverseString1("hello")

//*******************************************
function reverseString2(str) {
  //Single Line does it all
  return str.split("").reverse().join();
}

reverseString2("hello")


//*******************************************
function reverseString3(str) {    
  var final = "";
  for (var i = str.length - 1; i >= 0; i--) {
   final += str[i]
  }
  return final;
}

reverseString3("hello")

Author: Rick Cable / AKA Cyber Abyss

A 16 year US Navy Veteran with 25+ years experience in various IT Roles in the US Navy, Startups and Healthcare. Founder of FinditClassifieds.com in 1997 to present and co-founder of Sports Card Collector Software startup, LK2 Software 1999-2002. For last 7 years working as a full-stack developer supporting multiple agile teams and products in a large healthcare organization. Part-time Cyber Researcher, Aspiring Hacker, Lock Picker and OSINT enthusiast.

Leave a Reply