Convert strings to uppercase or lowercase in JavaScript

JavaScript has in-built functions for strings like slice(), substring(), and replace() methods. Here, we will use in-built methods like toUpperCase() and toLowerCase().

In this tutorial, we will convert a string into uppercase and lowercase with JavaScript.

Convert String Into Uppercase In JavaScript

String conversion to uppercase is frequently used in web development. Here, we store the value of the string into a variable and then bind the toUpperCase() method to that string variable. It will automatically convert a string into uppercase.

Here, the toUpperCase() method will convert the given variable value and return a new value as an upper string. Following is the syntax of the toUpperCase() method.

str.toUpperCase();

Let’s take an example to understand it.

var str = "this is simple string.";
strUpper = str.toUpperCase();

console.log(strUpper);

//Output:- THIS IS SIMPLE STRING.

Convert String Into lowercase In JavaScript

The toLowerCase() function works the same as the upper case function. We just have to bind this method with a string variable and it will convert string value into lowercase.

Here, the toLowerCase() method will convert the given variable value and return a new value as a lowercase string. Following is the syntax of the toLowerCase() method.

str.toLowerCase();

Let’s take an example to understand it.

var str = "THIS IS SIMPLE STRING.";
strLower = str.toLowerCase();

console.log(strLower);

//Output:- this is simple string.

Conclusion

In this article, we demonstrated how to convert a string value into lowercase or uppercase easily using the toUpperCase() and toLowerCase() methods.