JavaScript tricks

On the JavaScript learning journey, I have a lot of surprised moment about language features compare to my previous learned languages. I think those features are strong points of dynamic languages for more productivity but the trade-off are the clarity and consistent of language.

Let’s see the JavaScript tricks

Parse string to number: the function comes to mind is parseInt() or parseFloat() but the right function is Number() or add + prefix to variable. For example:

var a = “10”, b = “10abc”;
console.log(+a); // 10
console.log(Number(a)); // 10
console.log(parseInt(a)); //10
console.log(+b); // NaN
console.log(Number(b)); // NaN
console.log(parseInt(b)): // 10