Hacker News new | past | comments | ask | show | jobs | submit login

Can someone ELI5 the node code sample?

> full = full == "true";

Umm.. Why?

> if (!!limit

Not not limit? Is this due to some corner case conversion for JS to make 100% sure this gets into a boolean..?

I'm sure both samples have its reasoning, I'm just not aware. Any help please?




They are both ways to convert values to booleans. The top line would convert a string to a boolean. !! is a standard JS idiom to convert any value to its boolean equivalent, which can be somewhat complex due to the notions of "truthy" and "falsey" in JS.


Here is another hack we lazy devs used a lot back in the old days: double tilde operator[1]

[1]: https://stackoverflow.com/questions/5971645/what-is-the-doub...


“!!” is a way to convert any “falsy” value to a Boolean false. E.g. if you have a flag “enableFoo” that can take the values of undefined, null, numerical zero, NaN, empty string, and actual Boolean false, all these values will be converted to false with “not not”. All other values, including - ironically - “false” as a string, will be converted to Boolean true.


I even once got a `!!!someVariable` in a codebase I inherited. It's what convinced me that the previous devs weren't doing weird things to work around a bad backend: they just didn't know what they were doing


> full = full == "true";

That's basically a way to convert a stringified bool to a bool. Not the way I'd do it, and "assignee_name, summary, limit" are all declared as reassignable just to be able to reassign "full".

> if (!!limit

That's pretty common in js, not usually in if statements, but in other cases. No idea why that was done that way.


> full = full == "true"

When you send ?full=true in a query, your req.query object is { full: "true" }. It's not going to assume it can deserialize arbitrary strings.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: