209

I'm asking this just for the sake of shaving a few bytes.

I know I can use +x (unary plus) instead of Number(x). Is there a difference between those and parseFloat?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Namanyay Goel
  • 2,641
  • 3
  • 20
  • 26

5 Answers5

424

The difference between parseFloat and Number

parseFloat/parseInt is for parsing a string, while Number/+ is for coercing a value to a number. They behave differently. But first let's look at where they behave the same:

parseFloat('3'); // => 3
Number('3'); // => 3
parseFloat('1.501'); // => 1.501
Number('1.501'); // => 1.501
parseFloat('1e10'); // => 10000000000
Number('1e10'); // => 10000000000

So as long as you have standard numeric input, there's no difference. However, if your input starts with a number and then contains other characters, parseFloat truncates the number out of the string, while Number gives NaN (not a number):

parseFloat('1x'); // => 1
Number('1x'); // => NaN

In addition, Number understands hexadecimal input while parseFloat does not:

parseFloat('0x10'); // => 0
Number('0x10'); // => 16

But Number acts weird with empty strings or strings containing only white space:

parseFloat(''); // => NaN
Number(''); // => 0
parseFloat(' \r\n\t'); // => NaN
Number(' \r\n\t'); // => 0

On the whole, I find Number to be more reasonable, so I almost always use Number personally (and you'll find that a lot of the internal JavaScript functions use Number as well). If someone types '1x' I prefer to show an error rather than treat it as if they had typed '1'. The only time I really make an exception is when I am converting a style to a number, in which case parseFloat is helpful because styles come in a form like '3px', in which case I want to drop the 'px' part and just get the 3, so I find parseFloat helpful here. But really which one you choose is up to you and which forms of input you want to accept.

Note that using the unary + operator is exactly the same as using Number as a function:

Number('0x10'); // => 16
+'0x10'; // => 16
Number('10x'); // => NaN
+'10x'; // => NaN
Number('40'); // => 40
+'40'; // => 40

So I usually just use + for short. As long as you know what it does, I find it easy to read.

Nathan Wall
  • 10,530
  • 4
  • 24
  • 47
  • Also calling `parseFloat` on a number converts the number first into a string and then parses that string. `Number` skips that round trip. Can there be a case where such a round trip will lose precision? I don't know. – panzi Jul 10 '13 at 21:37
  • @Qantas94Heavy, it appears you're right. I'm guessing I was thinking of `parseInt('1e10')` (which returns `1`) and assumed `parseFloat` behaved the same way. Thanks for the correction! – Nathan Wall Dec 04 '13 at 14:13
  • 6
    I would not consider *whitespace=>0* behaviour of `Number()` as "weird" I would even consider it as more expected, whitespace is an empty value but it is not null/undefined => 0 is a nice result. Big (+) for you for the showcases anyway :) – jave.web Feb 06 '14 at 19:47
  • 5
    @NathanWall: Might want to mention that `Number('Infinity') === Infinity` whereas `parseInt('Infinity') === NaN` – sstur Aug 18 '14 at 21:51
  • 5
    I would not use `+` (unary plus) for this, because if you forget a semicolon on the previous line, an addition expression might be evaluated instead. – Jackson Apr 27 '17 at 23:36
  • 3
    For the cases they behave the same I've found out that parseFloat is from 1% to 15% slower, becoming slower when the number of decimal digits in a string increases. With 1M run in my system parseFloat('1.501') is 5% slower than Number('1.501'), and parseFloat('1.50137585467') is 15% slower than Number('1.50137585467'). So, I go for Number(). – bytepan May 09 '19 at 14:06
  • @sstur This is about `parseFloat` which works fine with `'Infinity'` – Andria Dec 30 '19 at 01:01
  • 2
    @ChrisBrownie55 Wow, good catch. I didn't know parseFloat can do that. I guess Infinity is not an integer! – sstur Dec 31 '19 at 18:49
  • I like to get the best of both with `Number(parseFloat("Some number string..."))` – Tim Oct 12 '20 at 04:51
17

The difference is what happens when the input is not a "proper number". Number returns NaN while parseFloat parses "as much as it can". If called on the empty string Number returns 0 while parseFloat returns NaN.

For example:

Number("") === 0               // also holds for false
isNaN(parseFloat("")) === true // and null

isNaN(Number("32f")) === true
parseFloat("32f") === 32
Jon
  • 428,835
  • 81
  • 738
  • 806
16

In these examples you can see the difference:

Number('') = 0;
Number(false) = 0;
Number('1a') = NaN;

parseFloat('') = NaN;
parseFloat(false) = NaN;
parseFloat('1a') = 1;

parseFloat is a bit slower because it searches for first appearance of a number in a string, while the Number constuctor creates a new number instance from strings that contains numeric values with whitespace or that contains falsy values.

micnic
  • 10,915
  • 5
  • 44
  • 55
2

For empty string, they are different.

+"" and Number("") returns 0, while parseFloat("") returns NaN.

xdazz
  • 158,678
  • 38
  • 247
  • 274
  • 2
    I would go so far as to say that `parseFloat()` has the right result as an empty string is NOT the number `0` (read: NaN) while a string with the character `"0"` in it IS `0`; – Christopher Sep 01 '12 at 12:19
  • `+x` returns `0` not only for an empty string but also for any whitespace-only strings. Examples: `+" "`, `+"\t\t\t"`, `+"\n\n"` - all of them give `0` as result – Lukasz Wiktor Oct 20 '16 at 12:36
2

As far as I know, and this is only overheard from colleagues so might be entirely ill informed, that parseFloat is marginally faster.

Though on further researching, it would seem that this performance difference is browser dependant.

http://jsperf.com/parseint-vs-parsefloat/6

Have a look at these jsPerf results, and make you're call. (it includes +x tests as well)

As noted in @xdazz 's answer, +"" and Number("") return 0 while parseFloat("") returns NaN so Again I would go with parseFloat, because an empty string does NOT mean the number 0, only a string with the character "0" in it means 0;

Christopher
  • 1,358
  • 17
  • 32
  • Here's a [more exhaustive test](http://jsperf.com/implicit-vs-explicit-string-to-float-conversion) fishing for a faster way to convert... `parseFloat()` is still the winner. – mindplay.dk Nov 15 '13 at 16:15