JavaScript Lowercase: Comprehensive Guide With 3 Examples

Are you tired of inconsistent casing in your JavaScript code? Want to standardize your strings to lowercase? Look no further than our comprehensive guide to JavaScript lowercase! With toLowerCase() method you can easily convert your strings to lowercase and improve the readability and consistency of your code. 

In this article, we’ll explore: 

  • What is Lowercase string?
  • 4 string lowercase examples
  • Convert string to lowercase except for the first character
  • What is the difference between .toLowerCase() and .toLocaleLowerCase()
  • How to safely handle the lowercase conversion, and more.

Let’s dive deep.

JavaScript Lowercase String

 Strings can be represented in either capital or lowercase letters in programming languages such as JavaScript. Lowercase refers to the small alphabetic characters, as opposed to uppercase letters.

Convert JavaScript String to lowercase

Let’s see how to make JavaScript String lowercase with different use cases.

1. Lowercase string using .toLowerCase()

The toLowerCase() string method is a built-in method in JavaScript that can convert a string to all lowercase  and returns a new string. The method can be applied to any strings in JavaScript and will return a new string with all the characters in lowercase. Original string remain unchanged.

Here is an example of how to use the .toLowerCase() method in JavaScript to lowercase all text:

let textToConvert = "JAVASCRIPT IS AWESOME!";
let lowerCaseString = textToConvert.toLowerCase();
console.log(lowerCaseString);
// Output: "javascript is awesome!"

In the above example, the string is converted to lowercase using toLowerCase() string method.

JavaScript toLowerCase() Method

2. Lowercase all but first letter

Assume you have a string with all of the text in capital letters. What if you only want to change the first letter of the string to lowercase?

Here is an example of how to use the .toLowerCase() method in JavaScript:

let textToConvert = "JAVASCRIPT IS AWESOME!";
let lowerCaseString = textToConvert.charAt(0).toUpperCase() + textToConvert.slice(1).toLowerCase();
console.log(lowerCaseString);
// Output: "Javascript is awesome!"

The chartAt(0) method returns the first character from a string. Using the toUpperCase() JavaScript method you can convert to upper case. The JavaScript slice() method will return a string starting from specified index to the end of the string. Using the toLowerCase() method you can convert it into lowercase.

3. Lowercase all except first letter of each word

You may need to convert a string to lowercase except for the first character of each word in a string. This is known as Title Case conversion. Here’s an example of converting a string to a Title Case:

let textToConvert = "JAVASCRIPT IS AWESOME!"
let words = textToConvert.split(' ');
let titleCase = words.map(w=> w.charAt(0).toUpperCase() + w.slice(1).toLowerCase())
console.log(titleCase.join(' '));
// Output: "Javascript Is Awesome!"
Lowercase All Except First letter Of Each Word
Lowercase All Except First letter Of Each Word

The .split() function divides strings into word arrays. Using the Array map() Method, change the first letter of each word to upper case and the remainder of the characters to lower case. Finally, connect all of the phrases.

4. Lowercase first letter

You may need to convert a string’s first letter alone to lowercase. Here’s an example of lowercase first letter:

let textToConvert = "JAVASCRIPT IS AWESOME!";
let lowerCaseString = textToConvert.charAt(0).toLowerCase() + textToConvert.slice(1);
console.log(lowerCaseString);
// Output: "jAVASCRIPT IS AWESOME!"

Difference between toLowerCase() and toLocaleLowerCase() methods

The toLowerCase() and toLocaleLowerCase() methods are used for converting a string to a lowercase. However, there is a difference:

toLowerCase() MethodtoLocaleLowerCase() Method
JavaScript toLowerCase() method converts a string to lowercase based on the Unicode standard.JavaScript toLocaleLowerCase() method converts a string to lowercase based on the locale-specific settings, which might result in different lowercase mappings than the Unicode standard.

Let’s take the Turkish word “DINÇ” and use both methods to convert it to lowercase and see the difference.

let textToConvert = "DINÇ";
console.log(textToConvert.toLowerCase());
// Output: "dinç"

console.log(textToConvert.toLocaleLowerCase('tr'));
// Output: "dınç"

Multiple languages can be selected on the browser’s settings page. You can choose your favorite language at the top of the list. There is no need to hard-code locale if your desired language is Turkish (tr). You can get it from the navigator object languages property.

let textToConvert = "DINÇ";

console.log(textToConvert.toLocaleLowerCase(navigator.languages[0]));
// Output: "dınç"
Browser Language Settings
Browser Language Settings

Troubleshoot toLowerCase() is not a function error

The error message “toLowerCase is not a function” or “toUpperCase is not a function” occurs when you try to use the toLowerCase or toUpperCase methods on data types that is not a string. These methods only work on strings, and using them on other data types like objects, arrays, booleans, or numbers will result in an error.

let textToConvert = [1,2,3]
console.log(textToConvert.toLowerCase());

// Output: TypeError: textToConvert.toLowerCase is not a function

To avoid this issue, use the JavaScript typeof() operator to determine the data type to ensure that the input type is a string before calling the toLowerCase() or toUpperCase() methods. The typeof() operator will return the input type as a string. If the input type is not a string, the following code will just output the message to the console and will not throw any exceptions.

let textToConvert = 'JAVASCRIPT'
if(typeof(textToConvert)==='string')
{
  console.log(textToConvert.toLowerCase());
}
else{ 
  console.log(`Input is not string type: ${typeof(textToConvert)}`) 
}

Difference between uppercase and lowercase

The main difference between uppercase and lowercase letters is their visual appearance. In some programming languages including JavaScript Uppercase and lowercase letters can also be treated differently, for example in case-sensitive searches.

let textToConvert = "JavaScript is Awesome!"
let lowerCase = textToConvert.toLowerCase();
console.log(lowerCase);
// Output: "javascript is awesome!"

let upperCase = textToConvert.toUpperCase();
console.log(upperCase);
// Output: "JAVASCRIPT IS AWESOME!"

Conclusion

In conclusion, JavaScript lowercase is a powerful method that can greatly improve the readability and consistency of your code. By converting your strings to lowercase, you can avoid confusion and easily identify matching values. 

With the help of this comprehensive guide and real-world examples, you should now have a solid understanding of how to implement JavaScript lowercase in your own code. 

In addition to converting a JavaScript string to lowercase, you learned about how to use toLowerCase() in various scenarios and the distinction between the toLowerCase() and toLocaleLowerCase() methods. 

How to fix a case conversion problem and the difference between lowercase and uppercase JavaScript strings!

Scroll to Top