• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / JavaScript / Reverse a String in JavaScript

Reverse a String in JavaScript

May 15, 2022 Leave a Comment

We can easily reverse a string in JavaScript using the JavaScript split(), reverse() and join() methods. Below is a function we will create to reverse a string.

function reverseString(str){
  var newStringArr = str.split("");
  newStringArr.reverse();
  newStringArr = newStringArr.join("");
  return newStringArr;
};

Here is the function in use with an example string:

function reverseString(str){
  var newStringArr = str.split("");
  newStringArr.reverse();
  newStringArr = newStringArr.join("");
  return newStringArr;
};

var someString = "Hello";
console.log(reverseWords(someString));

#Output:
olleH

When using string variables in JavaScript, we can easily perform string manipulation to change the values or order of the characters in our string.

One such manipulation is to reverse the characters in a string.

To reverse a string, we can first use the split() method to get an array of each character in the string, and then use the reverse() method to return the array with all of the characters in reverse.

After reversing the array we then join the characters back together using the JavaScript join() method.

Below is our function once again on how to reverse a string using JavaScript.

function reverseString(str){
  var newStringArr = str.split("");
  newStringArr.reverse();
  newStringArr = newStringArr.join("");
  return newStringArr;
};

Note that if we wanted to reverse a string with multiple words, we could use our same function. Here is an example:

function reverseString(str){
  var newStringArr = str.split("");
  newStringArr.reverse();
  newStringArr = newStringArr.join("");
  return newStringArr;
};

var someString = "This is an example string with words";
console.log(reverseWords(someString));

#Output:
sdrow htiw gnirts elpmaxe na si sihT

Also note that reversing a string is very similar to reversing words in a string, the main difference in the functions being that when using the split() and join() methods, we add a space split(” “), join(” “) when reversing words, and no space when reversing characters in a word, split(“”), join(“”).

Hopefully this article has been helpful for you to learn how to reverse a string using JavaScript.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Replace Multiple Characters in a String
  • 2.  Using JavaScript to Convert a String to Uppercase
  • 3.  Using JavaScript to Get the Decimal Part of a Number
  • 4.  JavaScript toLocaleString – Display the Date and Time Using JavaScript
  • 5.  Check if a String Contains Uppercase Letters in JavaScript
  • 6.  JavaScript Check If Number is a Whole Number
  • 7.  Using JavaScript to Compare Dates
  • 8.  How to Repeat a String in JavaScript
  • 9.  Remove Word From String In JavaScript
  • 10.  Using JavaScript to Change the Image src

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy