How To Convert All Array Elements To Uppercase In JavaScript

Convert all Array Elements to Uppercase in JavaScript

To convert all array elements to uppercase in JavaScript, we have three effective methods: using the toUpperCase() method with map() method, using the toUpperCase() method with for loop, and using the toUpperCase() method with split() and join() method. Check out the details below to get more information.

Convert all array elements to uppercase in JavaScript

Using the toUpperCase() method with map() method

The toUpperCase() method converts a string to uppercase letters. To convert all array elements to uppercase, you need to call the toUppercase() method to uppercase all elements. The map() method was introduced in another article. You can read it here if you want.

Syntax of toUpperCase():

string.toUpperCase()

Parameter: NONE

Return value: the returns value is the string converted to uppercase.

Code example:

var president = ['Trump', 'Bidden', 'Obama', 'Johnson'];
console.log("List the name of the president: ", president)

// Using the map() method and toUpperCase() method
var uppercased = president.map(name => name.toUpperCase());
console.log("After converting to uppercase: ", uppercased);

Output

List the name of the president:  [ 'Trump', 'Bidden', 'Obama', 'Johnson' ]
After converting to uppercase:  [ 'TRUMP', 'BIDDEN', 'OBAMA', 'JOHNSON' ]

Using the toUpperCase() method with for loop

The next way is using the toUpperCase() method with for loop. We create the function to loop all elements in the array and call the toUpperCase() method to uppercase all elements in the array. Let’s see the example below to get more information.

Example:

var president = ['Trump', 'Bidden', 'Obama', 'Johnson'];
console.log("List the name of the president: ", president)

// Create the function to loop all elements in an array and uppercase each element
for (var i = 0; i < president.length; i++) {
    president[i] = president[i].toUpperCase();
}

console.log("After converting to uppercase: ", president);

Output

List the name of the president:  [ 'Trump', 'Bidden', 'Obama', 'Johnson' ]
After converting to uppercase:  [ 'TRUMP', 'BIDDEN', 'OBAMA', 'JOHNSON' ]

Using the toUpperCase() method with split() and join() method

The split () and join() method is already built-in in JavaScript. The developer can easily call it out and use them with toUpperCase() method to uppercase all elements in the array.

Example:

var president = ['Trump', 'Bidden', 'Obama', 'Johnson'];
console.log("List the name of the president: ", president)

// Using the join() method, then uppercase the string and split it into an array
var presidentUp = president.join('|').toUpperCase().split('|');
console.log("After converting to uppercase: ", presidentUp);

Output

List the name of the president:  [ 'Trump', 'Bidden', 'Obama', 'Johnson' ]
After converting to uppercase:  [ 'TRUMP', 'BIDDEN', 'OBAMA', 'JOHNSON' ]

Summary

We have learned how to convert all array elements to uppercase in JavaScript with many ways. But we found that using the toUpperCase() method with the map() method is the easiest way. Hope the solutions outlined in the article can be of help to you.

Thank you for your read!

Maybe you are interested:

Leave a Reply

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