67

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?

Lucas
  • 16,930
  • 31
  • 110
  • 182

7 Answers7

162

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);
spender
  • 117,338
  • 33
  • 229
  • 351
  • is there a way to let `ans` retain its type, and not get converted into a string? – Lucas Sep 26 '12 at 23:02
  • 6
    Um... why on earth do you want to lowercase it then??? Anyway, ans is still a number when all this code has run. `.toString()` does not modify the original value (i.e. you're not storing it back to `ans`) – spender Sep 26 '12 at 23:04
  • 2
    @think123 I suggest that you shore up your understanding of the different javascript types and keep strings as strings and numbers as numbers. There's no need to be confused about the type of a variable. Where does the value of `ans` come from really? Why might the type of `ans` be unknown? – spender Sep 26 '12 at 23:09
  • 4
    @think123 Try : ``if(typeof ans == 'string') ans = ans.toLowerCase();`` – Gogol Jul 12 '14 at 08:03
  • Cannot read property 'toString' of undefined. I think we have to check type and then do this as Gogol mentioned. – Kurkula Feb 03 '17 at 00:31
  • check for the undefined first and then convert it to string. try : if(ans != undefined && ans != 'undefined' && ans != null) { ans = ans.tostring(); .... other logic} – S.Akruwala Feb 08 '17 at 07:13
7

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
David G
  • 94,763
  • 41
  • 167
  • 253
7

It's not an error. Javascript will gladly convert a number to a string when a string is expected (for example parseInt(42)), but in this case there is nothing that expect the number to be a string.

Here's a makeLowerCase function. :)

function makeLowerCase(value) {
  return value.toString().toLowerCase();
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
6
var ans = 334 + '';
var temp = ans.toLowerCase();
alert(temp);
Joe Coder
  • 4,498
  • 31
  • 41
4

It is a number, not a string. Numbers don't have a toLowerCase() function because numbers do not have case in the first place.

To make the function run without error, run it on a string.

var ans = "334";

Of course, the output will be the same as the input since, as mentioned, numbers don't have case in the first place.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
4
const myvar = 222;
const tempVar = myvar.toString().toLowerCase();
alert(tempVar);

Please use the following code because toLowerCase(); is applied on a string so first, you need to convert it into a string by using toString() method

Force Bolt
  • 1,117
  • 9
  • 9
2

You can use this too.

function strtolower(value){

 return ("" +  value).toLowerCase();
}

var ans=strtolower( 334); 
The concise
  • 444
  • 4
  • 12