DEV Community

Discussion on: 8 neat Javascript tricks you didn't know in 4 minutes.

Collapse
 
ajinspiro profile image
Info Comment hidden by post author - thread only visible in this permalink
Arun Kumar

Great one, Gil. I truly appreciate your js skills. But the author hasn't mentioned this in his post, has he ? He is simply promoting the use of unary operator for string conversion instead of using parseInt( ), FOR WHICH he is receiving EECOLOR's criticism (and mine for that matter).

Even though I accept your point that in the case you mentioned above, where use of unary + is better than parseInt - I strongly disagree this is a real world scenario. We explicitly make a string variable to number only when we actually know it is going to be a number. If a developer would knew there is going to be a comma between the numbers they would be replacing those commas with nothing, which will produce correct results.

Thread Thread
 
gilfewster profile image
Gil Fewster • Edited

I agree that relying on the coercion method alone is dangerous and liable to produce errors.

Which is why I said in my previous post that almost any method of type coercion is vulnerable to failure unless the data is carefully sanitised beforehand.

Some comments have been hidden by the post's author - find out more