How to convert string to uppercase JavaScript

In this post, we are going, on how to convert string to uppercase JavaScript with examples. The Javascript built-in function toUpperCase() function is used to convert a string to uppercase in Javascript. We will convert How to convert string to uppercase JavaScript, How to convert array to uppercase JavaScript, and How to convert string first letter to uppercase JavaScript.

1. Javascript toUpperCase()


The Javascript toUpperCase() function changes a string to uppercase. The JavaScript string toUpperCase() method does not change the original string instead it returns a new string converted to an upper case letter. It raises TypeError “toUpperCase is not a function” calling on null and undefined. Sometimes when calling on a value that is not a string, to solve this first we have to convert it to a string using the Tostring() and call the toUpperCase() method.

Syntax

string.toUpperCase()

How to convert string to uppercase JavaScript Example

let strMsg = 'welcome! to devenum.com, How are you';
console.log(strMsg.toUpperCase());

Output

WELCOME! TO DEVENUM.COM, HOW ARE YOU

2. Array toUppercase JavaScript


In this javascript example, we are going to learn How to convert string to uppercase JavaScript by using an array to upper case by using the toUpperCase() method. To convert all elements of the array to uppercase the array.map() function is used to iterate over the array and on each iteration call the toUpperCase() method to convert the array string element to upper case. After iteration is over the map() method will return an array of uppercase elements.

var myArr = ["dev","JavaScript","data","value"];
resArr = myArr.map(f=>{ return f.toUpperCase(); })
console.log(resArr);

Output

[ 'DEV', 'JAVASCRIPT', 'DATA', 'VALUE' ]

3.First letter toUppercase() JavaScript


To capitalize or uppercase the first letter of a string in javascript. We need to use two functions the first is the charAt(0). which converts the first letter to uppercase and slice() to return the string starting from the second letter. Let us understand with the below example

let strMsg = 'welcome!';

const firstCharUpper = strMsg.charAt(0).toUpperCase() + strMsg.slice(1)

console.log(firstCharUpper);

Output

Welcome!