string js reverse code example
Example 1: javascript string reverse
"this is a test string".split("").reverse().join("");
const reverse = str => [...str].reverse().join('');
const reverse = str => str.split('').reduce((rev, char)=> `${char}${rev}`, '');
const reverse = str => (str === '') ? '' : `${reverse(str.substr(1))}${str.charAt(0)}`;
reverse('hello world');
Example 2: reverse string javascript
const reverse = (str) => str.split("").reverse().join("");
function reverse1(str) {
const arr = str.split("");
arr.reverse();
const res = arr.join("");
return res;
}
function reverse2(str) {
let res = "";
for (let i = 0; i < str.length; i++) res = str[i] + res;
return res;
}
function reverse3(str) {
return str.split("").reduce((output, char) => {
return char + output;
}, "");
}