Using JavaScript ‘toLowerCase()’ String Method [with Examples]

JavaScript toLowerCase

Want to make a string all lower case in JavaScript? It’s the toLowerCase() method to the rescue!

The toLowerCase() method is part of the string object and has full support in all browsers.

In JavaScript, strings can either be a primitive or an object – primitives are the most basic kinds of variables, which are not objects, have no methods, and simply represent the given characters. However, String objects have useful methods for manipulating and measuring the string – like toLowerCase().

By default, JavaScript’s string variables are initialized as primitives – but will be automatically converted to objects by JavaScript when needed, so you aren’t likely to notice the difference.

JavaScript toLowerCase Syntax

mystring.toLowerCase()

Note that:

  • mystring is the text you wish to convert to lower case and can be any string variable
  • The original variable is not modified, so the result of calling toLowerCase() will need to be assigned to a new variable or used immediately
  • Your text will be returned with all characters converted to lower case
  • You can do the opposite with the toUpperCase() method – converting all characters to UPPERCASE

Example

To use toLowerCase(), simply call it from your existing string variable, assigning the result to a new variable:

var mystring = "Not the BEES!"; 
var result = mystring.toLowerCase(); 
console.log(result); // "not the bees!"

Conclusion

Converting a string’s case is frequently used when checking whether an email address or username is already in use in an authentication system or comparing values in arrays. It’s also useful when formatting other user input for storage in a database.

Click here for more JavaScript explainers!

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

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