Convert Strings to Uppercase or Lowercase in JavaScript is a common task when working with text. JavaScript provides several built-in methods for handling strings, including slice(), substring(), and replace(). Among these, the toUpperCase() and toLowerCase() methods are used to quickly change the text case. These methods help in formatting user input, displaying consistent data, and improving overall readability in web applications.
In this tutorial, we will look at practical examples of how to convert strings into uppercase and lowercase using these methods in 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.
When working with dynamic inputs, you might often need to verify whether a certain value exists in an array. jQuery makes this process much simpler with built-in methods. To dive deeper, check out our post on Value Exists in Array or not using jQuery for practical examples.
