• 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
  • Ruby
  • 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 Get the Host URL
  • 2.  Add Minutes to a Date Using JavaScript
  • 3.  Using JavaScript to Check if Variable is a Function
  • 4.  Using JavaScript to Get New Date Format in dd mm yy
  • 5.  JavaScript cos – Find Cosine of Number in Radians Using Math.cos()
  • 6.  Using JavaScript to Show a Div
  • 7.  Creating a JavaScript Function to Divide Two Numbers
  • 8.  Using JavaScript to Run a Function Every 5 seconds
  • 9.  How in JavaScript to Get the Day of the Week
  • 10.  How to Find the Longest String in an Array in JavaScript

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy