How to lowercase or uppercase all array values in JavaScript

published: 03 Jun 2022

2 min read

How to lowercase or uppercase all array values in JavaScript

In JavaScript, you can use the Array.map() method to iterate over all elements and then use the string methods to change the case of the elements.

Here is an example that demonstrates how to use the String.toUpperCase() method along with Array.map() to uppercase all elements in an array:

const names = ['Ali', 'Atta', 'Alex', 'John'];

const uppercased = names.map(name => name.toUpperCase());

console.log(uppercased);

// ['ALI', 'ATTA', 'ALEX', 'JOHN']

The toUpperCase() method converts a string to uppercase letters without changing the original string.

To convert all elements in an array to lowercase, you can use another JavaScript method called String.toLowerCase() as shown below:

const names = ['Ali', 'Atta', 'Alex', 'John'];

const lowercased = names.map(name => name.toLowerCase());

console.log(lowercased);

// ['ali', 'atta', 'alex', 'john']

Both string methods work in all modern browsers, and IE6 and above. However, the Array.map() was introduced in ES6 and only supports IE9 and up. On the other hand, arrow functions do not work in IE at all.

To support legacy browsers (IE9 and above), you should use the normal function instead:

const uppercased = names.map(function (name) {
    return name.toUpperCase();
});

const lowercased = names.map(function (name) {
    return name.toLowerCase();
});

To learn more about JavaScript arrays and how to use them to store multiple pieces of information in one single variable, take a look at this guide.

How to lowercase or uppercase all array values in JavaScript | Coding Tips And Tricks

Are we missing something?  Help us improve this article. Reach out to us.

Are you looking for other code tips?

Check out what's on in the category: javascript, programming
Check out what's on in the tag: javascript, programming, array