Javascript: transfer value to number

Created on 4 May 2017  Â·  2Comments  Â·  Source: airbnb/javascript

I don't understand why casting method +value is bad thing.
I saw this #transfer value to boolean issue because this guide says casting to boolean with !!value is best! But, Why +value is not? I need something deep explanation.

Thank you.

question

All 2 comments

+value and Number(value) are the same, as is !!value and Boolean(value).

However, ‼ has a huge precedence across many programming languages and logic systems to mean double negation - whereas + to cast to a number isn't nearly as universal.

I'd say that + and !! are both syntactic and thus more robust - however, using Number() is clearer than +, but Boolean() and !! are equally clear, and clarity is the most important thing. Therefore, I'd say you should use !! and Number().

@ljharb Okay, The cause is just that + isn't as universal.. I got it! Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zurfyx picture zurfyx  Â·  3Comments

ar
mbifulco picture mbifulco  Â·  3Comments

mismith picture mismith  Â·  3Comments

surfaceowl picture surfaceowl  Â·  3Comments

weihongyu12 picture weihongyu12  Â·  3Comments