How to lowercase or uppercase all array values in JavaScript
Today, we’ll explain to you how to lowercase or uppercase all array values in JavaScript. We have several ways to convert the values but in this short article, we will talk about the two different ways.
- Get the last item in an array using JavaScript
- How to truncate a string in JavaScript
- Merge or combine arrays using JavaScript
- Animated sticky header on scroll in JavaScript
1. Using array map() method
Using array map() method, we can iterate over all elements of the array and convert the case of the element as we want.
Let’s take an example for better understanding.
Convert an array values to LowerCase
Here, we will use the string method toLowerCase() along with an array map()
method to convert the all array values into the LowerCase.
let names = ["John", "William", "Elijah", "Michael"];
let convertedNames = names.map(name => name.toLowerCase());
console.log(convertedNames);
// Output: ["john", "william", "elijah", "michael"]
Convert an array values to UpperCase
The same way to convert the array values into UpperCase using the toUpperCase() method.
let names = ["John", "William", "Elijah", "Michael"];
let convertedNames = names.map(name => name.toUpperCase());
console.log(convertedNames);
// Output: ["JOHN", "WILLIAM", "ELIJAH", "MICHAEL"]
2. Using for loop
Let’s do the same task using the for
loop.
Convert an array values to LowerCase
let names = ["John", "William", "Elijah", "Michael"];
let convertedNames = [];
for (let i = 0; i
Convert an array values to UpperCase
let names = ["John", "William", "Elijah", "Michael"];
let convertedNames = [];
for (let i = 0; i
You can also use the below simple way if you have a large array of values.
let convertedNames = names.join('$$').toLowerCase().split('$$');
let convertedNames = names.join('$$').toUpperCase().split('$$');
I hope you find this article helpful.
Thank you for reading. Happy Coding..!!