JavaScript Convert String to UpperCase

Convert string all characters to uppercase in javaScript; Through this tutorial, i am going to show you how to convert string all characters to uppercase in javaScript.

Javascript String toUpperCase()

JavaScript String toUpperCase() is a built-in method that is used to convert all string characters into uppercase characters.

Syntax of javaScript string.toUpperCase() method; as shown below:

string.toUpperCase();

Explanation above syntax:

  • a string that you want to convert to upper case.
  • toUpperCase() is a method, which is used to convert string characters into upper case characters.

Example 1 – Convert All String Character To UpperCase in JavaScript

See the first example for convert all string character to uppercase in javaScript using toUpperCase() method; as shown below:

let str = 'a quick javascript tutorial';
console.log(str.toUpperCase()); // output: A QUICK JAVASCRIPT TUTORIAL

In the above example, All lowercase string characters converted into uppercase with the help of javascript toUpperCase() method.

Example 2 – Convert All Array String Character To UpperCase in JavaScript

To convert a string to uppercase characters, which is stored in an array?. You can see the following example:

let arr = [
  'Javascript',
  'PHP',
  'Mysql',
  'Sql'
]
let str = arr.join('~').toUpperCase()
let newArr = str.split('~')
console.log(newArr) //Output:  ["JAVASCRIPT", "PHP", "MYSQL", "SQL"]

In this example, we have used Javascript join() method the mixed-case array into a string, after that use toUpperCase() to convert the string characters to uppercase characters. And Javascript split() the string back into an array.

Note that, This method throws an exception if you call with null or undefined.

TypeError: Cannot read property ‘toLowerCase’ of undefined

In case, you pass the undefined string into toUpperCase() method and then you will get some error. The error looks like this: TypeError: Cannot read property ‘toLowerCase’ of undefined.

The following example:

let str = undefined
let res = str.toUpperCase();
console.log(res) // TypeError: Cannot read property ‘toLowerCase’ of undefined.

Example 3 – JavaScript uppercase Special Characters

In the following example; The special characters, digits, are not effect and string characters is convert to uppercase. See the following example for that:

let str = 'It iS a [email protected]@$$t Day.'
let string = str.toUpperCase();
console.log(string); //IT IS A [email protected]@$$T DAY.

Recommended JavaScript Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *