Computer >> Computer tutorials >  >> Programming >> Javascript

How do you reverse a string in place in JavaScript?


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>