function for reversing a string in javascript 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 in typescript
let toBeReversed: string = `.eslaf eb t'ndluow ecnetnes siht ,dehctiws erew eslaf dna eurt fo sgninaem eht fI`;
let charArray: string[] = toBeReversed.split('');
console.log(charArray);
let reverseArray: string[] = charArray.reverse();
console.log(reverseArray);
let reversedArray: string = reverseArray.join('');
console.log(reversedArray);
output:
If the meanings of true and false were switched, this sentence wouldn't be false.