How to Reverse a String JavaScript

How to Reverse a String JavaScript

Learn how to reverse a string javascript. It is one of the most asked interview questions. Reverse string means, printing a string in reverse order. For example, if you print the word “Hello” in reverse order, it will come out as “olleH”. There are no inbuilt functions for printing in reverse order. Let’s see how to print a string in reverseorder.

The Reverse string function is:

function reverseString(str) {
    return str.split("").reverse().join("");
}
  1.  Here, we have created a function with a parameter using the “function” keyword. We pass the String as a parameter.
  2. Then we named it for the function called “reverseString“. We pass the String as a parameter.
  3. Inside the function, we return the reverse order string.

print reverse string:

document.write(reverseString("Hello World"));

For every function in JavaScript, The output will be displayed only if we call the function.

Output:

how to reverse a string javascript

Leave a Reply