Covert All Letters of String to Lower Case in JavaScript

Strings are JavaScript’s data type used to represent the textual data and are most commonly used. Many people encounter a general requirement to make all letters of javaScript String in lowercase. This article will discuss different ways to convert all the letters to lowercase using various examples. We will also discuss how to convert to lowercase based on locale.

Javascript’s toLowerCase() will return the calling string converted to lowercase. If the calling value is not a string, it will be converted.

Table of Contents:

Make All Letters of String to Lower Case using toLowerCase() method

Example1:-

Convert the string to lowercase “JAVASCRIPT is A Versatile LAnguage”

Advertisements

Code:-

// create a new function lowerCaseConversion() 
function lowerCaseConversion(_value)
{
return String(_value).toLowerCase();
}
let stringVariable = "JAVASCRIPT is A Versatile LAnguage"
//usage of function lowerCaseConversion() 
lowerCaseConversion(stringVariable);

Output:-

javascript is a versatile language

Explanation:-

We are creating a function lowerCaseConversion(), within this function we use the toLowerCase() method of javascript. The input is first converted to String before applying toLowerCase().

Example2:-

Similarly, we can convert date values as well to lower case as the function lowerCaseConversion(_value) converts the input value to String before applying the toLowercase() method.

Convert a date to lowercase

Code:-

// create a new function lowerCaseConversion() 
function lowerCaseConversion(_value)
{
return String(_value).toLowerCase();
}
var dateVariable = new Date();
//usage of function lowerCaseConversion() 
console.log(lowerCaseConversion(dateVariable));

Output:-

mon jan 10 2022 18:46:46 gmt+0530 (india standard time)

Make All Letters of String to Lower Case using toLowerCase() based on Locale

Here locale is a language parameter based on the country. In a few countries, lower case values are different for different languages.

The toLocaleLowerCase() method returns the String value converted to lower case based on any specific locale.

Example:-

Convert the string to lowercase using Danish locale “JAVASCRIPT is A Versatile LAnguage”

Code:-

// create a new function lowerCaseConversion() 
function lowerCaseConversion(_value,_locale)
{
return String(_value).toLocaleLowerCase(_locale);
}
let stringVariable = "JAVASCRIPT is A Versatile LAnguage"
//usage of function lowerCaseConversion() 
console.log(lowerCaseConversion(stringVariable,"da-DK"));

Output:-

javascript is a versatile language

Explanation:-

  • We are creating a function lowerCaseConversion(), within this function we use the toLocaleLowerCase() method of javascript. We also pass the locale as the second argument, “da-DK,” for the Danish language.

Make All Letters of String to Lower Case using toLowerCase() using your own method

Apart from using the toLowerCase() method of javascript one can also write their own method.

Code:-

function lowerCaseConversion(_value) {
  let lowerString = '';
  let _string = String(_value);

  for (i = 0; i < _string.length; i++) {
    var charCode = _string.charCodeAt(i);
    if (charCode > 64 && charCode < 91) {
      lowerString += String.fromCharCode(charCode + 32);
    } else {
      lowerString += _string.charAt(i);
    }
  }
  return lowerString;
}

Example:-

Convert the string to lowercase “JAVASCRIPT is A Versatile LAnguage”

let stringVariable = "JAVASCRIPT is A Versatile LAnguage"
//usage of function lowerCaseConversion()
console.log(lowerCaseConversion(stringVariable));

Output:-

javascript is a versatile language

Explanation:-

  • Here, we are looping through the String and based on character code. We make all letters in lower case by adding their code value with 32 only If their value does not fall in the range of characters in lower case.
  • upperCaseCode + 32 = lowerCaseCode

I hope this article helped you convert all letters of a string to lowercase. Good Luck!!!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top