Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Weird API logging issue...

So... this is quite annoying and was pissing me off as I was trying to work on updating a script. Why does it do this? log (parseInt("a")); // Logs as null log ("" + parseInt("a")); // Logs as NaN
1499346089

Edited 1499346125
Jakob
Sheet Author
API Scripter
Because parseInt("a") is NaN (which, it seems, is logged as null), while "" + parseInt("a") converts the number parseInt("a") (i.e. NaN) to a string, so it is the the string "NaN".That is, what you have is the same as: log(NaN); // logged as null log("NaN"); // logged as NaN The only thing that's really strange here is the NaN logs as null...
1499346121
The Aaron
Pro
API Scripter
Our best guess at the implementation of log() is something like: const log = function(thing){ if(_.isString(thing)){ return console.write(thing); } return console.write(JSON.stringify(thing)); }; JSON doesn't have a representation for NaN.  In the first case, it gets converted to null as the "not a value" identifier for JSON.  In the second case, you are concatenating it with a string, causing NaN.toString() to be called, which returns "NaN".
Ah. Makes sense now. Thanks!
1499388598

Edited 1499388707
Lithl
Pro
Sheet Author
API Scripter
The Aaron said: Our best guess at the implementation of log() is something like: const log = function(thing){ if(_.isString(thing)){ return console.write(thing); } return console.write(JSON.stringify(thing)); }; JSON doesn't have a representation for NaN.  In the first case, it gets converted to null as the "not a value" identifier for JSON.  In the second case, you are concatenating it with a string, causing NaN.toString() to be called, which returns "NaN". Pretty sure the log implementation doesn't special-case strings, since the output includes quotes; JSON.stringify('a') produces "a" . Once upon a time, it was possible to look at the source code for the Roll20 library functions (eg, log(log.toString()) ), but Riley closed that after I mentioned it to him. And there I was not thinking to record anything for posterity. =P