How can I convert a string to an uppercase in JavaScript?
Rashid D
rashid d profile pic

In JavaScript, you can convert a string to uppercase using thetoUpperCase() method. Here's how you can achieve this:

1
2
3

const string = "hello world";
const uppercaseString = string.toUpperCase();

In this example,string.toUpperCase() converts thestring to uppercase and assigns the result to theuppercaseString variable. The resulting value will be"HELLO WORLD". ThetoUpperCase() method converts all the characters in the string to their uppercase equivalents according to the Unicode standard. It does not modify the original string but returns a new string with the uppercase characters. If you want to convert only specific parts of a string to uppercase, you can use additional methods likeslice() or regular expressions. For example:

1
2
3

const string = "hello world";
const uppercaseFirstLetter = string[0].toUpperCase() + string.slice(1);

Here,string[0].toUpperCase() converts the first character of the string to uppercase, andstring.slice(1) extracts the remaining characters. By concatenating the two parts, you get the result with the first letter capitalized:"Hello world". Keep in mind that thetoUpperCase() method is based on the Unicode standard, so the behavior may vary for characters from different languages or scripts.