Search code examples
javascriptstringuppercase

JavaScript - text with numbers to uppercase


I have seemingly random strings in javascript

var str = "abc123aaa2";

but I need to transform the letters to uppercase

var str = "ABC123AAA2";

I can't use any library, just vanilla JS. I tried using

str.toUpperCase(); 

but that returns undefined?

Can anyone help me with a speedy workaround?


Solution

  • It seems you are trying:

    var str = 'abc123aaa2';
    /* ...*/
    str.toUpperCase();
    /* some action on str */
    

    That's not how it works. Strings are immutable. You need to reassign str, if you want its value be the uppercased value:

    var str = "abc123aaa2";
    // later
    str = str.toUpperCase();
    // or at once:
    var str = "abc123aaa2".toUpperCase();