JavaScript Convert String to Uppercase

To convert a string to uppercase in JavaScript, call toUpperCase() method on the string. The expression returns a new string with all the characters of the calling string transformed to uppercase.

Syntax

The syntax to call toUpperCase() function on the string str is

str.toUpperCase()

toUpperCase() returns a new string with the contents of the original string transformed to uppercase.

Examples

In the following example, we take a string str, convert this string to uppercase, and display both the original string, and uppercase transformed string in #output.

index.html

<!DOCTYPE html>
<html lang="en">
<body>
    <pre id="output"></pre>
    <script>
        var str = 'hello world';
        var result = str.toUpperCase();
        var output = 'Original string  : ' + str;
        output += '\n\nUppercase string : ' + result;
        document.getElementById('output').innerHTML = output;
    </script>
</body>
</html>

Conclusion

In this JavaScript Tutorial, we learned how to transform a given string to uppercase in JavaScript, using toUpperCase() method, with examples.