Skip to main content

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'

Comments