GitHub 6219★

Swap case of characters in a string

JavaScript version

const swapCase = (str) =>
str
.split('')
.map((c) => (c === c.toLowerCase() ? c.toUpperCase() : c.toLowerCase()))
.join('');

TypeScript version

const swapCase = (str: string): string =>
str
.split('')
.map((c) => (c === c.toLowerCase() ? c.toUpperCase() : c.toLowerCase()))
.join('');

Examples

swapCase('Hello World'); // 'hELLO wORLD'
Follow me on and to get more useful contents.