How to Convert a String to Uppercase and Lowercase in JavaScript

How to Convert a String to Uppercase and Lowercase in JavaScript

During my early days of learning about web development. I had to waste time removing the text and starting over with my Caps-Lock button switched on to change a long sentence from uppercase to lowercase or vice versa.

In a bid to ensure you don’t go through this stress, hers's how to convert strings to uppercase and to lowercase.

Convert to Uppercase:

 let person = 'dennis';
  person.toUpperCase(); 
//DENNIS

Convert to Lowercase:

 let person = 'DENNIS';
  person.toLowerCase(); 
//dennis

In this article, you will learn how to easily convert a string from uppercase to lowercase or vice versa using the JavaScript methods, toLowerCase() and toUpperCase().

In the English language, we have so many ways of displaying text, this could be UPPERCASE, lowercase, Capitalized, CamelCase, and lots more. But for this guide, we are only concerned with UPPERCASE and lowercase.

How to Convert a String to Uppercase in JavaScript.

toUpperCase() is a method in JavaScript that convert only the value of the specified string to uppercase (capital letters).

Note: This JavaScript method, toUpperCase() and toLowerCase() doesn't receive parameters, toUpperCase(parameter)❌.

Let's create a new variable person to store our string in lowercase and then see how we can convert this string to uppercase:

Syntax:

let person = 'dennis';

To convert a string to uppercase, you will attach the toUpperCase() method to the variable with a dot (.):

person.toUpperCase();

To confirm our output, let’s use the browser console to display the output by using the JavaScript method, console.log().

console.log(person.toUpperCase()); 
// DENNIS

We can also create a reusable JavaScript function to implement the toUpperCase() method by passing in whatever string we wish to convert as an argument:

const person = (parameter) =>{
    console.log(arg.toUpperCase());
}

In the code above, a function with the named person was created, and then, we use console.log to display the value after converting the argument to uppercase with the toUpperCase() method.

Once we are creating the function, let's call the function while passing dennis as the argument.

person('dennis');
//DENNIS

How to Convert a String to Lowercase in JavaScript.

toLowerCase() is also a method in JavaScript that convert only the value of the specified string to lowercase (small letters).

In this section, we will use the same method that we used previously to convert a lowercase string to an uppercase string.

Since we want to convert an uppercase string to a lowercase string. Our code will look just like this:

let person = 'DENNIS';
console.log(person.toLowerCase());
//dennis

The only difference between this code and the previous one is that the toUpperCase() method has been replaced with the toLowerCase() method.

Let's also create a function to implement the toLowerCase() method.

const person = (parameter) =>{
    console.log(arg.toLowerCase());
}

person('DENNIS');
//dennis

Conclusion

In this article, you learned how you can easily convert a string to uppercase and lowercase using the toUpperCase() and toLowerCase() JavaScript methods respectively, and you also learned how to achieve this within a JavaScript function.

Thank you for taking the time to read my article.

Code with pleasure!