GitHub 6219★

Reverse a string

JavaScript version

const reverse = (str) => str.split('').reverse().join('');

// Or
const reverse = (str) => [...str].reverse().join('');

// Or
const reverse = (str) => str.split('').reduce((rev, char) => `${char}${rev}`, '');

// Or
const reverse = (str) => (str === '' ? '' : `${reverse(str.substr(1))}${str.charAt(0)}`);

TypeScript version

const reverse = (str: string): string => str.split('').reverse().join('');

// Or
const reverse = (str: string): string => [...str].reverse().join('');

// Or
const reverse = (str: string): string => str.split('').reduce((rev, char) => `${char}${rev}`, '');

// Or
const reverse = (str: string): string => (str === '' ? '' : `${reverse(str.substr(1))}${str.charAt(0)}`);

Examples

reverse('hello world'); // 'dlrow olleh'
Follow me on and to get more useful contents.