Odd toString behavior in javascript
Posted
by George
on Stack Overflow
See other posts from Stack Overflow
or by George
Published on 2010-05-20T15:31:50Z
Indexed on
2010/05/20
16:30 UTC
Read the original article
Hit count: 158
JavaScript
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?
© Stack Overflow or respective owner