Javascript toUpperCase()
In this article, we will take a look at how to convert a string to upper case using toUpperCase() method in javascript. This article will also cover how to convert first letter of a string to upper case.
[the_ad id=”651″] toUpperCase()
toUpperCase() method is called on a string object and returns a new string with all the characters of the calling string converted to upper case. Example,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let str = 'fox jumps';
let str_upper = str.toUpperCase();
console.log('Actual string: ' + str);
console.log('String in upper case: ' + str.toUpperCase());
let str = 'fox jumps'; let str_upper = str.toUpperCase(); console.log('Actual string: ' + str); console.log('String in upper case: ' + str.toUpperCase());
let str = 'fox jumps';
let str_upper = str.toUpperCase();
console.log('Actual string: ' + str);
console.log('String in upper case: ' + str.toUpperCase());

This prints

Actual string: fox jumps
String in upper case: FOX JUMPS

toUpperCase() converts only alphabets to upper case. Numbers and special characters remain unaffected. Example,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let str = 'fox jumps @1& wall';
let str_upper = str.toUpperCase();
console.log('Actual string: ' + str);
console.log('String in upper case: ' + str.toUpperCase());
let str = 'fox jumps @1& wall'; let str_upper = str.toUpperCase(); console.log('Actual string: ' + str); console.log('String in upper case: ' + str.toUpperCase());
let str = 'fox jumps @1& wall';
let str_upper = str.toUpperCase();
console.log('Actual string: ' + str);
console.log('String in upper case: ' + str.toUpperCase());

This prints

VM1770:3 Actual string: fox jumps @1& wall
VM1770:4 String in upper case: FOX JUMPS @1& WALL

Also, remember that toUpperCase() returns a new string, original string remains unchanged as can be seen from above examples.

Following example demonstrates that both the strings are different. It compares both the string using == operator.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let str = 'fox jumps';
let str_upper = str.toUpperCase();
console.log('Strings are same: ' + (str == str_upper));
let str = 'fox jumps'; let str_upper = str.toUpperCase(); console.log('Strings are same: ' + (str == str_upper));
let str = 'fox jumps'; 
let str_upper = str.toUpperCase(); 
console.log('Strings are same: ' + (str == str_upper));

This prints

Strings are same: false

[the_ad id=”656″] Convert first letter uppercase
To get the first letter of a string, use javascript string charAt() method.
charAt() takes a number as argument and returns the character at that index. First character is placed at index 0.
So, passing 0 as argument to charAt() returns the first character. Note that the return type of charAt() is a string.
Therefore, if we invoke charAt() followed by toUpperCase(), then first letter will be converted to upper case.

Next, append the remaining string to this capitalized first character. To get the remaining string, use any of javascript substring methods such as splice() method.
splice() method accepts an integer argument and returns the string with characters from index equal to the argument till the end of string. So, 'abcde'.splice(1) will return ‘bcde’.

Program example to make the first letter of a string to upper case is given below

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let str = 'fox jumps';
// get first character
let firstChar = str.charAt(0);
// get string without first character
let remaining = str.splice(1);
console.log('String with first character upper case: ' + firstChar.toUpperCase()+remaining);
let str = 'fox jumps'; // get first character let firstChar = str.charAt(0); // get string without first character let remaining = str.splice(1); console.log('String with first character upper case: ' + firstChar.toUpperCase()+remaining);
let str = 'fox jumps'; 
// get first character
let firstChar = str.charAt(0);
// get string without first character
let remaining = str.splice(1); 
console.log('String with first character upper case: ' + firstChar.toUpperCase()+remaining);

Output is

String with first character upper case: Fox jumps

[the_ad id=”644″] Hope the article was useful.