string reverse method in javascript code example
Example 1: javascript .split().reverse.join
"this is a test string".split("").reverse().join("");
function reverseString(string){
return string.split("").reverse().join("");}
reverseString("test string");
Example 2: javascript reverse a string
function reverseString(s){
return s.split("").reverse().join("");
}
reverseString("Hello");
Example 3: javascript reverse string without reverse method
function reverse(str){
let reversed = "";
for (var i = str.length - 1; i >= 0; i--){
reversed += str[i];
}
return reversed;
}
Example 4: javascript reverse string
function reverseString(str) {
if (str === "")
return "";
else
return reverseString(str.substr(1)) + str.charAt(0);
}
reverseString("hello");
Example 5: javascript how to reverse a string
function reverseString(str) {
var splitString = str.split("");
var reverseArray = splitString.reverse();
var joinArray = reverseArray.join("");
return joinArray;
}
reverseString("hello");
Example 6: reverse a word javascript
function reverseWords(str) {
const allWords = str.split(" ")
return allWords.map(item => item.split("").reverse().join("")).join(" ")
}