Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.toLowerCase not working, replacement function?

The .toLowerCase method is giving me an error when I try to use it on numbers. This is what I have:

var ans = 334; var temp = ans.toLowerCase(); alert(temp); 

And then it gives me this error:

'undefined' is not a function (evaluating 'ans.toLowerCase()') 

I don't know where I got this wrong. I always thought that numbers can also be parsed, with no change in result (maybe that's where I stuffed up).

But if that's not the error, can someone write a custom makeLowerCase function, to make the string lower case, perhaps using regex or something?

like image 714
Lucas Avatar asked Sep 26 '12 22:09

Lucas


2 Answers

The .toLowerCase() function only exists on strings.

You can call .toString() on anything in JavaScript to get a string representation.

Putting this all together:

var ans = 334; var temp = ans.toString().toLowerCase(); alert(temp); 
like image 81
spender Avatar answered Oct 06 '22 19:10

spender


Numbers inherit from the Number constructor which doesn't have the .toLowerCase method. You can look it up as a matter of fact:

"toLowerCase" in Number.prototype; // false 
like image 31
David G Avatar answered Oct 06 '22 18:10

David G