Joined December 2013
·
Posted to
Converting a string into an integer in JavaScript*
over 1 year
ago
@hauleth - yeah, I know it's a Number
after all. The point of that tip was to show the different ways of solving single problem and using bitwise operations is just a fast way: http://jsperf.com/math-floor-vs-math-round-vs-parseint/33
I also wanted to point the differences between those methods so readers can be aware of those and avoid surprises in the future.
But thanks to you I have learned something new today - thank's for that ;]
Posted to
Converting a string into an integer in JavaScript*
over 1 year
ago
@hauleth - it really is simple however it doesn't convert a string to an integer but to a floating-point number:
str = '3.75';
+str; // returns 3.75
@jailbot - all numbers in JavaSript are instances of
Number
.Also NaN (not-a-number) is an instance of a
Number
.https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number
Edit
One note here:
If you type e.g.
x = 3;
x
won't be an instance of Number. It won't be an object at all. But if you try do deal with it as with a Numberx
will be converted to Number on the fly.