How to convert all Array values to LowerCase, UpperCase in JavaScript

How to convert all Array values to LowerCase, UpperCase in JavaScript

Return all values from array in LowerCase or UpperCase using for loop instead of map in Javascript

You can also now achieve this very simply by using an arrow function and the map() method of the Array:

var words = ['Foo','Bar','Fizz','Buzz'].map(v => v.toLowerCase());
console.log(words);
var words = ['Foo','Bar','Fizz','Buzz'].map(v => v.toUpperCase());
console.log(words);

Note that map() will only work in browsers which support ES2015. In other words, anything except IE8 and lower.

Similarly, arrow functions will not work at all in IE. For a legacy browser safe version you would need to use an anonymous function:

var words = ['Foo','Bar','Fizz','Buzz'].map(function(v) {
  return v.toLowerCase();
});
console.log(words);
var words = ['Foo','Bar','Fizz','Buzz'].map(function(v) {
  return v.toUpperCase();
});
console.log(words);

With arrays, the += operator does not do what you expect - it calls .toString on the array and concatenates them. Instead, you want to use the array push method:

var sorted = [];
for (var i = 0; i < words.length; i++) {
    sorted.push(words[i].toLowerCase());
}
console.log(sorted);
var sorted = [];
for (var i = 0; i < words.length; i++) {
    sorted.push(words[i].toUpperCase());
}
console.log(sorted);

If you want fast and have a very large array of words

sorted=words.join('|').toLowerCase().split('|');
sorted=words.join('|').toUpperCase().split('|');

Let's share!

javascript lowercase uppercase

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

The essential JavaScript concepts that you should understand

The essential JavaScript concepts that you should understand - For successful developing and to pass a work interview

JavaScript Uppercase the First Letter of a String

In this tutorial, we will learn how to uppercase the first letter of a string in JavaScript and Node.js

Grokking Call(), Apply() and Bind() Methods in JavaScript

In this article, we will have a look at the call(), apply() and bind() methods of JavaScript. Basically these 3 methods are used to control the invocation of the function.

JavaScript Tutorial: if-else Statement in JavaScript

This JavaScript tutorial is a step by step guide on JavaScript If Else Statements. Learn how to use If Else in javascript and also JavaScript If Else Statements. if-else Statement in JavaScript. JavaScript's conditional statements: if; if-else; nested-if; if-else-if. These statements allow you to control the flow of your program's execution based upon conditions known only during run time.

How to Retrieve full Profile of LinkedIn User using Javascript

I am trying to retrieve the full profile (especially job history and educational qualifications) of a linkedin user via the Javascript (Fetch LinkedIn Data Using JavaScript)