How to convert string to upper and lowercase in JavaScript

Key takeaways:

  • The toLowerCase() method converts all characters in a string to lowercase and returns a new string without altering the original. This is useful for case-insensitive comparisons or standardizing text.

  • Similarly, the toUpperCase() method transforms all characters to uppercase and is useful for emphasis or display purposes.

  • JavaScript lacks a built-in capitalize method, but we can implement one by first converting the string to lowercase and then changing the first letter to uppercase. This method standardizes text with a capitalized first letter and lowercase for the rest.

  • Changing the case of strings is commonly needed in JavaScript for consistency, easier comparisons, and formatted display.

Working with strings is a common requirement in JavaScript, and sometimes we need to change the casing—either to uppercase or lowercase. Changing string cases helps with data consistency, easy comparisons, and enhanced display formatting. Let’s learn how we can easily perform these conversions, understand why they’re useful, and even try out some code examples.

Converting strings to lowercase

In JavaScript, we can use the toLowerCase() method to convert a string into a lowercase string. The toLowerCase() method will return a new string.

let str = "UPPER Case";
let lowerCaseStr = str.toLowerCase();
console.log(lowerCaseStr);

In the above code:

  • Line 1: We define a string variable str with the value UPPER Case.

  • Line 2: We use the toLowerCase() method on str to convert all characters to lowercase, storing the result in lowerCaseStr.

  • Line 3: We log the lowercase version, which outputs upper case.

Converting strings to uppercase

In JavaScript, we can also use the toUpperCase() method to convert a string into an uppercase string. The toUpperCase() method will return a new string.

var str = "uppercase";
var upperCaseStr = str.toUpperCase();
console.log(upperCaseStr);

In the above code:

  • Line 1: We define a string variable str with the value uppercase.

  • Line 2: We use the toUpperCase() method on str to convert all characters to uppercase, storing the result in upperCaseStr.

  • Line 3: We log the lowercase version, which outputs UPPERCASE.

Capitalize a string

We don’t have any in-build method to capitalize a string. However, we can implement our own method. The steps to capitalize a string are:

  • Convert the string to lowercase

  • Replace the first letter with an uppercase letter

function capitalize(str) {
const lowerCaseString = str.toLowerCase(), // convert string to lowercase
firstLetter = str.charAt(0).toUpperCase(), // uppercase the first character
strWithoutFirstChar = lowerCaseString.slice(1); // remove first character from lowercase string
return firstLetter + strWithoutFirstChar;
}
console.log(capitalize("javaScript"));
console.log(capitalize("educative.io"));

In the above code:

  • Lines 1–7: We define the capitalize() function to capitalize the string. Inside this function:

    • Line 2: We convert the entire string to lowercase for consistency.

    • Line 3: We capitalize only the first character.

    • Line 4: We remove the first character from the lowercase string.

    • Line 6: We combine the capitalized first letter with the rest of the lowercase string, returning the result.

  • Lines 9–10: We call the capatilize() function on two strings: javaScript and educative.io, and log the output. We can see the Javascript and Educative.io in the console.

Knowledge test

Let’s attempt a short quiz to assess your understanding.

1

Which method will you use to convert hello world to uppercase?

A)

toLowerCase()

B)

toUpperCase()

C)

toString()

Question 1 of 20 attempted

Conclusion

Converting string cases in JavaScript is simple but powerful for ensuring data consistency, enabling case-insensitive comparisons, and formatting content. By learning toUpperCase() and toLowerCase(), we can handle text with greater flexibility. Let’s keep practicing and exploring new ways to manage strings in JavaScript!


Frequently asked questions

Haven’t found what you were looking for? Contact Us


How to convert all uppercase to lowercase in JavaScript

Use the toLowerCase() method, which converts every character in the string to lowercase. For example:

let text = "HELLO";
let lowerText = text.toLowerCase(); // "hello"

How do I change the case of a string in JavaScript?

JavaScript provides toUpperCase() and toLowerCase() methods to change the case. You can use these methods to convert the entire string to either uppercase or lowercase as needed:

let text = "Hello";
let upperText = text.toUpperCase(); // "HELLO"
let lowerText = text.toLowerCase(); // "hello"

How to convert lowercase to uppercase without using string function in JavaScript?

You can manually convert lowercase to uppercase by checking each character’s ASCII code. Here’s one way to do it:

function toUpperCaseWithoutMethod(str) {
    let result = "";
    for (let i = 0; i < str.length; i++) {
        let charCode = str.charCodeAt(i);
        if (charCode >= 97 && charCode <= 122) { // ASCII range for lowercase letters
            result += String.fromCharCode(charCode - 32); // Convert to uppercase by adjusting ASCII code
        } else {
            result += str[i]; // Keep character unchanged if not lowercase
        }
    }
    return result;
}

console.log(toUpperCaseWithoutMethod("hello")); // "HELLO"

Free Resources