Sunday 9 December 2018

JavaScript: Convert a string to number without using parseInt, parseFloat

You can convert a string to number using + (Unary) operator.

HelloWorld.js
var x = (+'1.1') + (+0);
var y = (+'123') + (+0);

console.log("Value of x is " + x);
console.log("Value of y is " + y);

Output
Value of x is 1.1
Value of y is 123

Previous                                                 Next                                                 Home

No comments:

Post a Comment