How do you reverse a string in place in JavaScript?

JavascriptWeb DevelopmentFront End Technology

To reverse a string in JavaScript, try to run the following code, which reverses a string “qries”

Example

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <script>
         var myStr;
         function reverseStr(myStr) {
            if(!myStr.trim() || 'string' !== typeof myStr) {
               return;
            }
            let l=myStr.length, s='';
            while(l > 0) {
               l--;
               s+= myStr[l];
            }
            return s;
         }
         document.write("Reversed String: " +reverseStr("qries"));
      </script>
   </body>
</html>
raja
Published on 30-Jan-2018 06:06:29
Advertisements