To convert all string characters into uppercase characters; In this tutorial, you will learn everything about JavaScript toUpperCase() method and how to convert all string characters to uppercase using toUpperCase() method.
Javascript String toUpperCase()
JavaScript str.toUpperCase() method converts the characters of a string to uppercase characters.
Note that, this method does not affect any of the special characters, digits, and string characters that are already in uppercase.
The following syntax represents the string.toUpperCase() method:
string.toUpperCase();
Here,
- a string that you want to convert to upper case.
- toUpperCase() is a method, which is used to convert string characters into upper case characters.
Let’s take a look at the example:
let str = 'a quick javascript tutorial'; console.log(str.toUpperCase()); // output: A QUICK JAVASCRIPT TUTORIAL
In this example, you can see all the lowercase string characters converted into uppercase with the help of javascript toUpperCase() method.
Javascript toUpperCase() array
How to convert a string to uppercase characters, which is stored in an array?. You can see the following example:
let arr = [ 'Javascript', 'PHP', 'Mysql', 'Sql' ] let str = arr.join('~').toUpperCase() let newArr = str.split('~') console.log(newArr) //Output: ["JAVASCRIPT", "PHP", "MYSQL", "SQL"]
In this example, we have used Javascript join() method the mixed-case array into a string, after that use toUpperCase() to convert the string characters to uppercase characters. And Javascript split() the string back into an array.
Note that, This method throws an exception if you call with null
or undefined
.
TypeError: Cannot read property ‘toLowerCase’ of undefined
In case, you pass the undefined string into toUpperCase() method and then you will get some error. The error looks like this: TypeError: Cannot read property ‘toLowerCase’ of undefined.
The following example:
let str = undefined let res = str.toUpperCase(); console.log(res) // TypeError: Cannot read property ‘toLowerCase’ of undefined.
JavaScript uppercase Special Characters
In the above mention, this method does not affect any of the special characters, digits, and string characters that are already in uppercase. See the following example for that:
let str = 'It iS a xyz@@$$t Day.' let string = str.toUpperCase(); console.log(string); //IT IS A XYZ@@$$T DAY.