3

Assuming I have a numerical string:

var foo = "0";

Assume I want to parse it as a number, and increment it assigning the value to bar (just an arithmetical example), I know I could do:

var bar = parseInt(foo, 10)+1;

But I believe the above example could be simpler and more readable. Consider this example:

//alternative 1
var bar = +foo+1;

And this one:

//alternative 2
var bar = ++foo;

Both of these will also yield the same result for bar (the latter will also increment foo, but that's no problem).

My question is, by using one of these alternatives, would I be exposing my code to the same possible implementation-dependant flaws of using parseInt without specifying a radix/base?

Or are these safe to use as well?

Fiddle for testing.

Fabrício Matté
  • 69,329
  • 26
  • 129
  • 166
  • 3
    http://stackoverflow.com/a/4606789/405017 – Phrogz Jun 16 '12 at 22:04
  • 1
    As far as I'm concerned, `var bar = parseInt(foo, 10) + 1` IS the most readable form of that statement. – Karl Nicoll Jun 16 '12 at 22:06
  • What is the source of your numerical string? User-supplied input? – Phrogz Jun 16 '12 at 22:07
  • @Phrogz There's no source, this is just a theological example. I got intrigued as how javascript handles forced typecasting, your linked answer is very useful. I'm setting that table as my wallpaper now. – Fabrício Matté Jun 16 '12 at 22:09
  • Does [Comparison between all number-conversion methods](http://stackoverflow.com/a/8112802/938089?are-there-are-any-side-effects-of-using-this-method-to-convert-a-string-to-an-in) answer your question? – Rob W Jun 16 '12 at 22:09
  • 1
    possible duplicate of [Are there are any side effects of using this method to convert a string to an integer](http://stackoverflow.com/questions/8112757/are-there-are-any-side-effects-of-using-this-method-to-convert-a-string-to-an-in) – Phrogz Jun 16 '12 at 22:10
  • @RobW Thank you, I guess my google skills didn't get me there. There's really a ton of possibilities there to study. I'm intrigued now however, how come `parseInt` of `d = '0020'` is `16`? Is it due to the lack of `radix`? – Fabrício Matté Jun 16 '12 at 22:13
  • 1
    @FabrícioMatté Yes. Prefixing zero = octal, prefixing `0x` = hexadecimal. Otherwise, it's 10-based (decimal). The prefixed zeros are often a source of errors (e.g. when using 0s to pad). Hence, prefixing zeros is discouraged/deprecated (in strict mode, an error is thrown): `'use strict'; 01` -> "SyntaxError: octal literals and octal escape sequences are deprecated" – Rob W Jun 16 '12 at 22:14
  • @RobW thanks for the answer! (I've never seen octals in real coding before), and thanks everyone for the links, it will be very useful for (my) future reference. `:)` – Fabrício Matté Jun 16 '12 at 22:22
  • Another thing you can do is divide the string by 1, ie, `var num = "123"/1;`. I don't know any potential side effects, but I know for most cases I've looked at in the last few days, it's worked out well enough. – Seiyria Feb 19 '14 at 15:45
  • @Seiyria thanks, yes and `*1` is fine too, it just loses at readability. Since this is an almost 2 years old question, I'll add that nowadays I use mostly the unary `+` operator for converting from string to number. It is common practice, simple and minification-friendly. – Fabrício Matté Feb 19 '14 at 18:23

1 Answers1

3

From the perspective of readability and maintainability parseInt is the best choice. It clearly tells what it does and does it reliably.

Tomorrow, when a fellow developer joins the project, of after a couple of months when you yourself will be looking at your own code, or if you suddenly decide to increment by ten instead of just one, you'll be in less trouble working with parseInt.

Also, try JSLint. It's worth it.

Oleg
  • 9,341
  • 2
  • 43
  • 58
  • 1
    Yes, after reading all the comments it seems that `parseInt` is more readable and maintainable, and I do work mostly on group projects too. Thanks for the help everyone. – Fabrício Matté Jun 16 '12 at 22:15
  • I recommend [JSHint](http://jshint.com) over JSLint, because it allows more filters. JSLint complains too much. – Rob W Jun 16 '12 at 22:17
  • @RobW I think it's a matter of personal preference. We are all free to choose what fits best. I prefer JSLint because it's more strict and because I think Douglas did a lot of good thinking there. Besides, you can disable quite a lot of checks in it as well. – Oleg Jun 16 '12 at 22:20