HowTo: Use the JavaScript toLowerCase method

In this HowTo i’ll demonstrate how the JavaScript tolowerCase method works.

What is the JavaScript toLowerCase method?

The answer is kinda in the name of the method! The JavaScript toLowerCase method is an available method of a string to return an all lower case version of it.

"SHOUTING LOUDLY!".toLowercase();
// Returns "shouting loudly!"

There’s not much else to say regarding the toLowerCase method. It doesn’t take any parameters and can only be called on strings. As with other JavaScript methods, toLowerCase doesn’t actually modify a variable that it may be called on. Therefore if you want to make the change permanent, you will need to assign the result of the toLowerCase method to the same or a new variable.

var message = "ARE YOU SURE?";

message = message.toLowerCase();

// message = "are you sure?"

One example of when you might want to use the JavaScript toLowerCase method is when you have a string that is all uppercase and you just want to capitalise the first word whilst making the rest lower case. There are few ways to do this. Probably what I would do is keep the first letter in the string and then call toLowerCase on every other letter in the string. This can be done with the slice() method.

var string = "HELLO TO YOU!";

var normalText = string.slice(0,1) + string.slice(1).toLowerCase();

// normalText = 'Hello to you!'

The first slice adds the uppercase ‘H’ to the normalText variable. The second slice adds the rest of the string but transforms it to lower case. This is assuming that the first letter of string is uppercase of course :) You can also do something similiar using the toUpperCase() method too.

Conclusion

Probably one of the most simple methods available to use on strings, the JavaScript toLowerCase method completely changes a string to lower case. This is regardless of whether the string already has any upper/lower case letters in it. Don’t forget, it order to make the change permanent the toLowerCase method needs to assign it’s result to a variable.