Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 501
SoluableNonagon Avatar asked Aug 30 '26 17:08

SoluableNonagon


1 Answers

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();
like image 128
KooiInc Avatar answered Sep 01 '26 07:09

KooiInc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!