Ufuk's Blog

How to reverse a string in JavaScript

☕️ 1 min read

I neither like nor do this kind of algorithms scripting/programming. For the future needs, I thought it makes sense to have a list of these notes. I might never need it but I believe there is at least someone in the world who is searching the web to find a quick answer for his/her little problem when developing some kind of application. Of course there are lots of way to do the same job, this one is my approach.

function reverseStr(str) {
  // declaring an empty array
  const arr = [];

  // pushing letters to the array in a reverse order
  for (let i = str.length - 1; i >= 0; i--) {
    arr.push(str.charAt(i));
  }

  // converting array to a string
  const reversedStr = arr.join('');

  return reversedStr;
}

reverseStr('Hello,  World!');