Is Javascript's toUpperCase() language safe?

Will Javascript's String prototype method toUpperCase() deliver the naturally expected result in every UTF-8-supported language/charset?

I've tried simplified chinese, south korean, tamil, japanese and cyrillic and the results seemed reasonable so far. Can I rely on the method being language-safe?


"イロハニホヘトチリヌルヲワカヨタレソツネナラムウヰノオクヤマケフコエテアサキユメミシヱヒモセス".toUpperCase() > "イロハニホヘトチリヌルヲワカヨタレソツネナラムウヰノオクヤマケフコエテアサキユメミシヱヒモセス"

Edit: As @Quentin pointed out, there also is a String.prototype.toLocaleUpperCase() which is probably even "safer" to use, but I also have to support IE 8 and above, as well as Webkit-based browsers. Since it is part of ECMAScript 3 Standard, it should be available on all those browsers, right?

Does anyone know of any cases where using it delivers naturally unexpected results?


What do you expect?

JavaScript's toUpperCase() method is supposed to use the "locale invariant upper case mapping" as defined by the Unicode standard. So, basically, "i".toUpperCase() is supposed to be I in all cases. In cases where the locale invariant upper case mapping consists of multiple letters, most browsers will not upper case them correctly, for example "ß".toUpperCase() is often not SS.

Also, there are locales that have different uppercase rules than the rest of the world, the most notable example being Turkish, where the uppercase version of i is İ (and vice versa) and the lowercase version of I is ı (and vice versa).

If you want that behaviour, you will need a browser that is set to Turkish locale, and you have to use the toLocaleUpperCase() method.

Also note that some writing systems have a third case, "title case", which is applied to the first letter of a word when you want to "capitalize" it. This is also defined by the Unicode standard (for example, the Title case of the ligature njis Nj while the upper case is NJ), but (as far as I know) not available to JavaScript. Therefore if you try to capitalize a word using substring and toUpperCase, expect it to be wrong in rare cases.

Yes. From the spec:

[Returns] a String where each character is either the Unicode uppercase equivalent of the corresponding character of [the input] or the actual corresponding character of [the input] if no Unicode uppercase equivalent exists.

For the purposes of this operation, the 16-bit code units of the Strings are treated as code points in the Unicode Basic Multilingual Plane. Surrogate code points are directly transferred from [input to output] without any mapping.

The result must be derived according to the case mappings in the Unicode character database (this explicitly includes not only the UnicodeData.txt file, but also the SpecialCasings.txt file that accompanies it in Unicode 2.1.8 and later).

So while this might not exactly match your languages expectations (as many languages use the same characters but not necessarily in the same way), it does certainly deliver the naturally expected result as specified in the Unicode Character Database.

Category:javascript Time:2018-11-05 Views:1

Related post

  • Is JavaScript an application language? 2009-08-15

    I have always thought of JavaScript as a client-side scripting tool for augmenting the functionality of HTML, which in turn is usually generated by some other server-side technology - Java, .NET, Rails, Django, PHP, etc. Recently though I have heard

  • (Javascript- HTML Parser) Can be html parsed with javascript without server languages? 2010-07-01

    Can html be parsed with javascript without server languages? my webpage should do : parsing of all images from the url that user enter. Can I do it only with Javascript if yes with library functions that exist doing that? Thanks, --------------Soluti

  • Write JavaScript in Other Languages 2009-05-08

    I have some clients who are not English speaking. They would like the JavaScript I write for them to be in another language. Can browsers understand other languages, or am I limited to non-English comments? navigateur.nomApp.indice("Microsoft") Inste

  • Is JavaScript an untyped language? 2009-06-08

    I've found that some people call JavaScript a "dynamically, weakly typed" language, but some even say "untyped"? Which is it really? --------------Solutions------------- JavaScript is untyped: Even Brendan Eich says so. On Twitter, he replied to a th

  • javascript : make a new safe class constructor 2010-12-25

    sometimes we loss the new keyword when define new object, obj = new Clazz(); //correct obj = Clazz(); //wrong, but no syntax error, hard to debug. I want to write a function to help me create Class and make it new safe. var Class = function(){ var co

  • Cannot use 'javascript' because another language has been specified earlier in this page (or was implied from a CodeFile attribute) 2011-02-28

    I have a website in which I am including a javascript file from this link: Code Snippet Page I am getting this error: Microsoft JScript runtime error: Object expected Here is my code: <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master"

  • Can I prevent stupid errors in JavaScript because the language does not require semicolons? 2011-09-07

    I just lost quite a bit of time because I changed a JavaScript function that read (something like) function F(a,b,c) { return x(a,b,c) + y(a,b,c) + z(a,b,c); } to function F(a,b,c) { return // x(a,b,c) + y(a,b,c) + z(a,b,c); } when I needed to test s

  • Facebook javascript in different languages 2011-11-27

    My facebook javascript that I added to uses the "like" button from facebook and other components, looks like this: <div id="fb-root"></div> <script> window.fbAsyncInit = function() { FB.init({ appId : '164355773607006', // App ID ch

  • How to post-load images after document load with JavaScript in template languages such as HAML? 2012-01-18

    Having found a seemingly excellent solution for loading images after a document's loaded here: $(function(){ $.each(document.images, function(){ var this_image = this; var src = $(this_image).attr('src') || '' ; if(!src.length > 0){ //this_image.s

  • Is it possible to convert Visual Basic.NET to JavaScript or a language that runs on Mac 2012-10-28

    I am trying to convert Visual Basic.NET to JavaScript. Is this possible? --------------Solutions------------- Yes, it's possible. The development environment will have more difference than the languages, so it will take more than a direct translation

  • When serving JavaScript files, is it safe to gzip it by default 2009-05-18

    The question fits in the title. I am not interested in what the spec recommend but what the mix of browsers currently deployed support the best. Google Docs gzips their JS. The Google AJAX Libraries API CDN gzips JS. Yahoo gzips the JS for their YUI

  • Javascript in Machine Language 2010-05-28

    Is it possible to convert javascript code to machine code or pure native binary? --------------Solutions------------- Not sure why anyone would want to do this, but there is an example here of how to generate a .NET executable file from JScript (not

  • Chrome Extension /Javascript- detecting user language in Chrome Browser 2010-08-27

    What I need to know is: self explaining image :) Is this language selection possible in the Chrome Browser? --------------Solutions------------- Would the internalization API work for you? If you wan

  • Escape JavaScript in Expression Language 2011-07-07

    I've the following piece of inline JavaScript code in a Facelets page: <script> var webName = '#{_selectedItem.item.webName}'; </script> This fails with a JS syntax error when the EL expression evaluates to a string containing an apostrop

  • javascript convert string to safe class name for css 2011-10-02

    I am sure this must of been asked before but can't find any in the search. What is the fastest way to ensure all non safe characters are removed from a string allowing it to be used in a CSS class name? --------------Solutions------------- I would re

  • Json: PHP to javascript safe or not? 2011-07-25

    I understand that the use of eval(json_str) on the client is vulnerable to malicious code. My question is, if json_str was an array constructed by the php function json_encode, would I be safe? e.g. json_str = json_encode(array(record1, record2, reco

  • What good template language is supported in JavaScript? 2008-09-24

    Templates are a pretty healthy business in established programming languages, but are there any good ones that can be processed in JavaScript? By "template" I mean a document that accepts a data object as input, inserts the data into some kind of ser

  • Javascript book which teaches javascript the language, not dom 2009-07-03

    I am a pretty good programmer(IMO only, of course. Know Python, Java well. Tried my hands at Lisp, Ruby, Haskell). I also know how to use Jquery reasonable well, so I know Dom manipulation. I want a recommendation on a Javascript book, which teaches

  • How to convert javascript regex to safe java regex? 2011-01-24

    strOutput.replace("/{{[^]*?}}/g",""); Is there a way to convert JavaScript regexes to Java-safe regexes? The above statement gives me the error: Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ ) I'm not all that familiar with regex, s

Copyright (C), All Rights Reserved.

processed in 1.147 (s). 13 q(s)