JavaScript String toUpperCase()
Syntax & Examples
String.toUpperCase() method
The toUpperCase() method of the String class in JavaScript returns the calling string value converted to uppercase.
Syntax of String.toUpperCase()
The syntax of String.toUpperCase() method is:
toUpperCase()
This toUpperCase() method of String returns the calling string value converted to uppercase.
Return Type
String.toUpperCase() returns value of type String
.
✐ Examples
1 Using toUpperCase() method
In JavaScript, the toUpperCase()
method converts a string to uppercase letters.
For example,
- We define a string variable
str
with the value'hello world'
. - We use the
toUpperCase()
method to convert the string to uppercase. - The result is stored in the variable
upperStr
. - We log
upperStr
to the console using theconsole.log()
method.
JavaScript Program
const str = 'hello world';
const upperStr = str.toUpperCase();
console.log(upperStr);
Output
HELLO WORLD
2 Using toUpperCase() method on a string with mixed case
In JavaScript, the toUpperCase()
method can be used to convert a string with mixed case letters to all uppercase letters.
For example,
- We define a string variable
str
with the value'JavaScript'
. - We use the
toUpperCase()
method to convert the string to uppercase. - The result is stored in the variable
upperStr
. - We log
upperStr
to the console using theconsole.log()
method.
JavaScript Program
const str = 'JavaScript';
const upperStr = str.toUpperCase();
console.log(upperStr);
Output
JAVASCRIPT
3 Using toUpperCase() method on a string with numbers and symbols
In JavaScript, the toUpperCase()
method only affects alphabetic characters and leaves numbers and symbols unchanged.
For example,
- We define a string variable
str
with the value'Hello 123!'
. - We use the
toUpperCase()
method to convert the alphabetic characters to uppercase. - The result is stored in the variable
upperStr
. - We log
upperStr
to the console using theconsole.log()
method.
JavaScript Program
const str = 'Hello 123!';
const upperStr = str.toUpperCase();
console.log(upperStr);
Output
HELLO 123!
Summary
In this JavaScript tutorial, we learned about toUpperCase() method of String: the syntax and few working examples with output and detailed explanation for each example.