DEV Community

Ben Haugen
Ben Haugen

Posted on

Reversing a String in JavaScript

I am going to write a weekly blog on some commonly asked questions in coding interviews. They will start very basic and progress from there. It is important to continue to practice solving algorithms in your job search or the knowledge that you have learned self-teaching or at a bootcamp will recede faster than you think.

One of the most commonly asked questions when interviewing for junior level web-development positions is how to reverse a string. There are, in fact, many ways to reverse a string in JavaScript and some ways are definitely better than others but today let's just learn the two simplest ways.

Quick and easy

The easiest way to reverse a string is going to be with built-in JavaScript functions. If you solve it this way in an interview, make sure you show the interviewer that you can solve it without these built-in functions as well. It won't hurt to show that you know how to solve it like this because it proves you know some basic functions. Now let's get to it.

The three JS methods that we are going to use are .split(), .reverse() and .join(). These method definitions are pulled directly from MDN Web Docs, which is a great source for all JS methods.

  • The split() method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.

  • The reverse() method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.

  • The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

If we want to reverse the string "dinosaur", we can actually chain all three methods together below, like so:

Using a for loop

The interviewer is going to want to see that you know how to write a simple for loop, so show them that you know how. First you are going to want to set a variable equal to an empty string so you can add to it after the for loop. When writing the for loop, remember that you want to start it at the end of the string. So instead of your typical i = 0, you want to start at the last index, which you can accomplish by doing i = str.length - 1. And remember, we want to decrement i here because we want to go from end to beginning.

Inside that for loop, we want to add to our empty variable that we made at the start. The shortcut for adding to our empty variable will look something like this: reversed += str[i].

And don't forget to return that variable AFTER the for loop is complete but INSIDE our function. This is what it should look like:

This is pretty basic stuff but make sure you commit it to memory and are practicing your algorithms everyday. Sometimes we put so much effort into trying to secure a job interview that, when we finally do, we have forgotten some of the basics. Don't let that happen to you!

Top comments (0)