javascript

convertToUppercase()

Parameters: string: String

A string input that the function will transform.

Returns: A string transformed to uppercase.

The convertToUppercase function in JavaScript enables us to convert a string to uppercase letters. By taking an input string, the function transforms all the alphabetical characters in that string into their equivalent uppercase versions.

Methods
String Manipulation
Easy dificulty

Creating a Convert-To-Uppercase Function in JavaScript

Greetings, programmer! This post is meant for those entering the combining world of code. We will explore a function in JavaScript which converts text to uppercase. Low key, clear, without regional dialects and devoid of slang, our aim is to provide a straightforward guide. Happy learning and happy coding!

Step 1

Start by declaring a new function and call it convertToUpperCase. This function should take one argument str. This str argument is the string that we want to convert to uppercase.

function convertToUpperCase(str) {

}

Step 2

Inside this function, use the string method toUpperCase. This method returns a new string where all the lower case letters in the original string are converted to upper case.

function convertToUpperCase(str) {
  return str.toUpperCase();
}

Step 3

Now we can test our function with a sample string to make sure it's working as expected.

console.log(convertToUpperCase('hello world')); // should print 'HELLO WORLD'

Step 4

Make sure to handle edge cases. What if the input string is already in uppercase or the string is empty? The above function would still work because in JavaScript, toUpperCase method doesn't affect the non alphabetic characters and makes the whole string to uppercase including the already upper case letters.

console.log(convertToUpperCase('HELLO WORLD')); // should print 'HELLO WORLD'
console.log(convertToUpperCase('')); // should print ''

Step 5

In conclusion, we've created a convertToUpperCase function in JavaScript to convert any given string to uppercase. Here is the final implementation of the function:

function convertToUpperCase(str) {
  return str.toUpperCase();
}

This function is simple and should cover all scenarios as the toUpperCase method is a built-in method provided by JavaScript that handles various cases.

Learn function in:

String Transformation

This concept involves transforming a string to uppercase.

Learn more

Mathematical principle

There's no specific mathematical principle involved in converting a string to uppercase. However, understanding ASCII values can provide insight. Each character has a specific ASCII value. In ASCII, the difference between a lower case letter and its upper case equivalent is 32. However, JavaScript handles this conversion for us.

Learn more