4 ms·
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
by BossingAround 7y ago
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?
- deleted 7y ago[deleted]
- hn_throwaway_99 7y agoThey 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.
- SahAssar 7y ago> 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.
- mynegation 7y ago“!!” 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.
- williamdclt 7y agoI 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
- hombre_fatal 7y ago> 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.
- longnguyen 7y agoHere 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-double-tilde-operator-in-javascript https://stackoverflow.com/questions/5971645/what-is-the-doub...