Data Structure and Algorithms in JavaScript

Data Structure and Algorithms in JavaScript

2019, Apr 02    

How to reverse a string in JavaScript

Method 1: Use the build-in functions: split, reverse and join

function reverse(str) {
  return str.split('').reverse().join('');
}

Method 2: Use the for loop

function reverse(str) {
  let res = '';
  for (let i = str.length - 1; i >= 0; i--) {
    res += str[i];
  }
  return res;
}