Difference between toLocaleLowerCase() and toLowerCase()
Unlike toLowerCase
, toLocaleLowerCase
takes localization into account. In most cases, with most languages, they will produce similar output, however certain languages will behave differently.
Check out the description on MDN:
The
toLocaleLowerCase()
method returns the value of the string converted to lower case according to any locale-specific case mappings.toLocaleLowerCase()
does not affect the value of the string itself. In most cases, this will produce the same result astoLowerCase()
, but for some locales, such as Turkish, whose case mappings do not follow the default case mappings in Unicode, there may be a different result.
For completeness, toUpperCase
and toLocaleUpperCase
behave likewise, except with upper-casing.
Now for the issue with your snippet not doing anything. There are actually 2 issues.
-
These methods return new strings and don't modify the original (JavaScript strings are immutable). You will need to re-assign the value back to the element.
-
UPDATE:innerText
is non-standard, and will not work across all browsers. UsetextContent
instead, and only addinnerText
to support old versions of IE.innerText
is now standardized, though it was not at the time this answer was posted.
Working Snippet:
function ByLocale() {
var el = document.getElementById("demo");
el.textContent = el.textContent.toLocaleLowerCase();
}
function ByLower() {
var el = document.getElementById("demo");
el.textContent = el.textContent.toLowerCase();
}
<p>Click the button to convert the string "HELLO World!" to lowercase letters.</p>
<button onclick="ByLocale();">By Locale LowerCase</button>
<button onclick="ByLower();">By LowerCase</button>
<p id="demo">HELLO World!</p>
Zameer Ansari
Updated on July 05, 2022Comments
-
Zameer Ansari almost 2 years
I was trying to fiddle with
toLocaleLowerCase()
andtoLowerCase()
methods.function ByLocale() { document.getElementById("demo").innerText.toLocaleLowerCase(); } function ByLower() { document.getElementById("demo").innerText.toLowerCase(); }
<p>Click the button to convert the string "HELLO World!" to lowercase letters.</p> <button onclick="ByLocale();">By Locale LowerCase</button> <button onclick="ByLower();">By LowerCase</button> <p id="demo">HELLO World!</p>
My questions are:
- What is Locale, because both the functions seems to return similar output?
- What is the difference between these two methods?
- Why is the fiddle code not getting executed?
-
Zameer Ansari over 8 yearsAnd what is the bug in the code I'm trying to run
-
Alexander O'Mara over 8 years@student One second, I'll add that also.
-
Everts over 8 yearsWith English, the two methods give the same output. I'd guess it is relevant with languages like French where cap letters do not take accent in Switzerland but does in France by convention.
-
Alexander O'Mara over 8 years@student Added a working snippet with explanation.
-
Zameer Ansari over 8 years@AlexanderO'Mara Does it also apply to toLocaleUpperCase and toUpperCase?
-
Legends about 4 years@AlexanderO'Mara You should have added a
turkish
word as an example to demo the difference ;-) -
Mingwei Samuel almost 3 years