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.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like.

  1. Get the length of a Map in JavaScript
  2. Delete an element from a Map in JavaScript
  3. Get the first element of a Map in JavaScript
  4. Get an element from a Map using JavaScript
  5. Update an element in a Map using JavaScript
  6. Add an element to a Map in JavaScript