-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Boolean() vs !! #113
Comments
|
|
I'll take the opportunity to add the other very common case (convert a numerical string to an integer): |
|
|
Prefer |
I personally find !! very unreadable and looks slightly hacky. Most of us know what it means but not everyone. Compare !!someAray to Boolean(someArray). The difference with Boolean(something) - it's instantly readable and obvious to what it's doing. |
|
For serverside code this makes sense but clientside code where bytes matter then |
From looking at another culture repository (airbnb) https://github.com/airbnb/javascript#coercion--booleans |
Don't JS compressors take care of that kind of thing for you @dancork? |
I thought compressors/minifiers only touched arguments and scoped vars and functions. |
That's probably true @dancork. I am not an expert, hence the question. UglifyJS doesn't seem to do it. However, this seemed such an obvious thing that could be automated, instead of having humans worrying about it, that I couldn't believe someone hadn't done it. The Google Closure Compiler seems to do exactly that: var x = 0;
console.log(Boolean(x)); after running through the var x=0;console.log(!!x); So I would argue there are options, even for the smallest possible payloads in the front-end, that don't sacrifice readability, and allow us to have a consistent coding style everywhere. |
That's a good shout @pmcnr-hx maybe one for a project lounge :) |
I don't think number of characters should bear that much weight even when it's frontend code; tooling has been mentioned but also once the asset is gzip-compressed, I've mentioned in the past the |
And as such, why use |
@antoinerousseau those behave a little differently on the edge cases |
@ptcc true, sometimes you need to really "parse" and sometimes you need a different base than 10, but for other cases |
Yes @antoinerousseau I agree! |
this is my prefered way to cast a something to an integer Also be aware: !!new Boolean(false) // true
!!Boolean(false) // false |
|
@tomsoir, there's nothing "wrong" with |
@dT-Nick yes it was worth mentioning it, although
In that case it acted like In my opinion, parseInt should only be used when you want an integer with a radix (base) different than 10. If you want a float or a regular integer, use something more explicit and adapted, and be readable for the next person who reads your code :) And remember there is no dedicated Read more: https://stackoverflow.com/a/20513389/1052033 |
https://www.measurethat.net/Benchmarks/Show/11127/0/boolean-vs Moreover, I am not sure that while |
now why do you say that |
for the benchmark here https://www.measurethat.net/Benchmarks/Show/11127/0/boolean-vs |
Old question |
They behave a bit differently in TypeScript
|
As the title says. Add your reaction to the one you prefer and drop a comment if you wish to give a reasoning.
The text was updated successfully, but these errors were encountered: