Odd toString behavior in javascript
- by George
I have this small function that's behaving oddly to me. Easy enough to work around, but enough to pique my curiosity.
function formatNumber(number,style) {
if (typeof style == 'number') {
style = style.toString();
}
return (number).format(style);
}
The return format part is based on another function that requires the style variable to be a string to work properly, so I'm just checking if style is a number and if it is to convert it to a string. When the function above is written as is, the format function format doesn't work properly. However when I write it as simply:
return (number).format(style.toString());
Everything works. Is there a difference between putting the .toString function inside the format call vs performing it before hand and setting it as the variable style?