Transform Text From One Case to Another Using JavaScript

Introduction

 
This article will describe converting text from one case to another using JavaScript. In general programming, we need to convert text to the following cases:
  • Lower Case
  • Upper Case
  • Title Case
We will see the conversion one by one below.
 
To convert a text into lowercase using JavaScript: 
  1. function toLowerCase(str) {  
  2.     return str.toLowerCase();  
You can use this as in the following:
  1. alert(toLowerCase("HELLO WORLD")); 
To convert text into uppercase using JavaScript:
  1. function toUpperCase(str) {  
  2.     return str.toUpperCase();  
You can use this as in the following:
  1. alert(toUpperCase("hello world")); 
To convert text into Titlecase using JavaScript:
  1. function toTitleCase(str) {  
  2.     return str.replace(/\w\S*/g,  
  3.     function (txt) {  
  4.         return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();  
  5.     });  
You can use this by calling:
  1. alert(toTitleCase("hello world")); 

Summary

 
Hope you all enjoyed reading this article. Let me know your thoughts and views via comments.


Rebin Infotech
Think. Innovate. Grow.