Menu

gitpiper

swapCase javascript Code Snippet in 2023

stringbeginner

Last Updated: 1 March 2023

Creates a string with uppercase characters converted to lowercase and vice versa.

  • Use the spread operator (...) to convert str into an array of characters.
  • Use String.prototype.toLowerCase() and String.prototype.toUpperCase() to convert lowercase characters to uppercase and vice versa.
  • Use Array.prototype.map() to apply the transformation to each character, Array.prototype.join() to combine back into a string.
  • Note that it is not necessarily true that swapCase(swapCase(str)) === str.
const swapCase = str => [...str] .map(c => (c === c.toLowerCase() ? c.toUpperCase() : c.toLowerCase())) .join('');
swapCase('Hello world!'); // 'hELLO WORLD!'

javascript snippet similar to swapCase For You in February 2023

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2023 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️