Using JavaScript toUpperCase() method you can convert a string to uppercase letters. This method does not change the original string. This method returns the string converted to uppercase.
string.toUpperCase()
console.log('alphabet'.toUpperCase()); // 'ALPHABET'
Example JavaScript toUpperCase
Simple example code.
<!DOCTYPE html>
<html>
<body>
<script>
let text = "Hello World! toUpperCase";
let result = text.toUpperCase();
console.log(result)
</script>
</body>
</html>
Output:
Another example
Let’s say you have a form where users enter their names, and you want to display the entered name in uppercase letters on the webpage. You can use the toUpperCase()
method to convert the user’s input to uppercase before displaying it. Here’s an example using JavaScript and HTML:
<!DOCTYPE html>
<html>
<head>
<title>Uppercase Name</title>
<script>
function convertToUppercase() {
let input = document.getElementById("nameInput").value;
let uppercaseName = input.toUpperCase();
document.getElementById("result").textContent = uppercaseName;
}
</script>
</head>
<body>
<h1>Uppercase Name Converter</h1>
<label for="nameInput">Enter your name:</label>
<input type="text" id="nameInput">
<button onclick="convertToUppercase()">Convert</button>
<p id="result"></p>
</body>
</html>
You can try running the code in a browser and see how the entered name gets converted to uppercase when you click the “Convert” button.
Comment if you have any doubts or suggestions on this JS basic method.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version