Coder Perfect

Is it possible to convert a JavaScript string to entirely lower case?


What is the best way to convert a JavaScript string value to all lowercase letters?

“Your Name” to “Your Name” as an example.

Asked by Derek

Solution #1

var lowerCaseName = "Your Name".toLowerCase();

Answered by John Topley

Solution #2

Use the String object’s toLowerCase or toLocaleLowerCase methods. The distinction is because toLocaleLowerCase considers the user/current host’s locale. The ECMAScript Language Specification (ECMA-262) states that toLocaleLowerCase…


var lower = 'Your Name'.toLowerCase();

It’s also worth noting that the toLowerCase and toLocaleLowerCase functions have been designed to work with any value type. As a result, you can use these functions on things that aren’t Strings. This will result in the automatic conversion of each character in the resulting string value to a string value before altering the case of each character in the resulting string value. For example, you can apply to LowerCase on the following date:

var lower = String.prototype.toLowerCase.apply(new Date());

and which is essentially the same as:

var lower = new Date().toString().toLowerCase();

For its simplicity and readability, the second form is often favored. The first had the advantage of working with a null value in earlier versions of Internet Explorer. When toLowerCase or toLocaleLowerCase is applied to null, the outcome is null (and not an error condition).

Answered by Atif Aziz

Solution #3

Yes, the toLowerCase() method on any string in JavaScript returns a new string that is the old string in all lower case. The old string will be left alone.

As an example, you could do the following:


Answered by pkaeding

Solution #4

Lower case or toLocaleUpperCase() routines do not perform as they should.

For example, on my machine, Safari 4, Chrome 4 Beta, and Firefox 3.5.x, strings including Turkish characters are wrongly converted.

“en-US”, “tr”, and “en-US” are the browsers’ responses to navigator.language.

However, as far as I could tell, there is no method to access the user’s Accept-Lang setting in the browser.

Only Chrome causes me problems, despite the fact that I have set all of my browsers to use the tr-TR locale.

These options, I believe, only influence HTTP headers, but we don’t have access to them via JS.

“The characters within a string are transformed to… while preserving the current locale,” according to Mozilla’s documentation.

This will produce the same result as…” in most languages.

I believe it is correct for Turkish; it makes no difference whether the language is set to en or tr.

It should convert “DN” to “dinç” and “DIN” to “dnç” or vice versa in Turkish.

Answered by sanilunlu

Solution #5

Just some examples for toLowerCase() and toUpperCase(), as well as a prototype for toTitleCase() and toProperCase(), which aren’t yet accessible ()

edited 2018

Answered by ewwink

Post is based on