Convert All Array Elements To Lowercase In JavaScript

To convert all array elements to lowercase in JavaScript –

  1. Use the map() function to iterate over the array and call the toLowerCase() method on each element.
  2. Use the forEach() function to iterate over the array and call the toLowerCase() method on each element.
  3. Use a for loop to iterate over the array and call the toLowerCase() method on each element.

Let’s look at each of these methods in detail below.

Convert All Array Elements To Lowercase In JavaScript

convert all array elements to lowercase in javascript

1) map() Method

The map() method is used to create a new array with the results of calling a function on every element in the original array.

We can use this method to convert all array elements to lowercase.

Example:

var arr = [“JavaScript”, “is”, “awesome”];

arr = arr.map(function(element) {

  return element.toLowerCase();

});

console.log(arr); // [“javascript”, “is”, “awesome”]

In the above code, we have declared an array with three elements – “JavaScript”, “is”, and “awesome”.

Then, we have used the map() method to iterate over each element in the array and call the toLowerCase() method on it.

Finally, we have logged the new array to the console.

2) forEach() Method

The forEach() method is used to execute a function on each element in an array.

We can use this method to convert all array elements to lowercase.

Example:

var arr = [“JavaScript”, “is”, “awesome”];

var newArr = [];

arr.forEach(function(element) {

  newArr.push(element.toLowerCase());

});

console.log(newArr); // [“javascript”, “is”, “awesome”]

In the above code, we have declared an array with three elements – “JavaScript”, “is”, and “awesome”.

We have then used the forEach() method to execute the toLowerCase() method on each element in the original array and push the results to a new array.

Finally, we have logged the new array to the console.

3) for Loop

We can also use a for loop to convert all array elements to lowercase.

Example:

var arr = [“JavaScript”, “is”, “awesome”];

var newArr = [];

for(var i = 0; i < arr.length; i++) {

  newArr.push(arr[i].toLowerCase());

}

console.log(newArr); // [“javascript”, “is”, “awesome”]

In the above code, we have declared an array with three elements – “JavaScript”, “is”, and “awesome”.

We have then used a for loop to execute the toLowerCase() method on each element in the original array and push the results to a new array.

Finally, we have logged the new array to the console.

You can use any of the above methods to convert all array elements to lowercase in JavaScript.

I am a fan of map() method because there is no need to declare an additional array to store the results. Also, forEach() is not supported in Internet Explorer.

If you are not comfortable with map() or forEach() method, then you can always use a for loop. A for loop is not as fast as map() or forEach() method but it is supported in all browsers.

Hope this helps. 🙂

Leave a Comment