How to use Array.form() and reverse() Methods In Javascript
The Array.from() is used to convert the string into an array of individual characters. The reverse() method is then applied to reverse the order of the elements in the array. Finally, the join() is used to combine the reversed elements back into a string.
Example: The below code example explains the above approach practically.
// Function to reverse string
function reverseString(str) {
const strRev = Array.from(str).reverse().join("");
console.log(strRev);
}
// Function call
reverseString("w3wiki");
reverseString("JavaScript");
reverseString("TypeScript");
Output
skeeGrofskeeG tpircSavaJ tpircSepyT
Reverse a String in JavaScript
We have given an input string and the task is to reverse the input string in JavaScript. It is a very common question asked in a JavaScript interview. There are various methods to reverse a string in JavaScript, which are described below with examples.
Examples:
Input: str = "w3wiki"
Output: "skeeGrofskeeG"
Input: str = "Hello"
Output: "olleH"