Fun with JavaScript: the mystery of the failing parse

See if you can figure that one out in less than ten seconds, in which case kudos to you… What does this code return?

['1', '2'].map(parseInt)

If you answered

[1, 2]

I'm sorry to say that you're as wrong as I was when I wrote this bug.

Now if you answered

[1, NaN]

well, congratulations! So what’s going on here?

The `Array.prototype.map` method calls the passed-in function with three parameters: the current array item, its index, and the array. The `parseInt` function can take two parameters: the string to parse, and the radix. If you forget about that second parameter, you may think that the additional parameters that `map` passes in would get ignored. Not so in this case. `parseInt` is called twice. The first time with the parameters ('1', 0) (the third one will be ignored), the second time with ('2', 1). The radix is supposed to be between 2 and 36 (why 36? I don’t know). Radix zero gets ignored and works like the default, while 1 yields `NaN`.

The moral of the story is, don’t try to be too clever. The code above can be more explicitly, and more correctly written like so:

['1', '2'].map(function(n) {return parseInt(n, 10);})

6 Comments

  • The max radix is 36 because that would be the number of different values when using all numbers (0-9 = 10) and characters (a-z = 26). Just a small FYI... :)

  • Ah thanks, that makes sense.

  • Unless you really need parseInt and all of its radix-based glory, you could do the same with Number:

    [ "1", "2" ].map(Number)

    Works just fine and gives [ 1, 2 ].

  • Ha! Didn't think of that one. Thanks!

  • Hello Bertrand,
    This query is regarding Orchard CMS which is an open source enterprise community software. From Orchard project portal I got the link of your blog - http://www.orchardproject.net/about.

    One of our enterprise The customer has unstructured data spread across discrete location in the form of documents(.doc, .pdf, .ppt) that needs to be migrated.

    This will be have to be stored on ShareDrive and Orchard needs to interface with it.

    The points we are specifically interested are..

    a) Scalability of the software and whether it has any in build database that can store documents. Currently our customer has 15000 documents?
    b) What all operation can be performed using Orchard?
    c) Whether there are any enterprise that deployed this software?
    d) Do you have any enterprise support team ?
    e) Is Orchard is a CMS for individuals & small organizations ?

    This is bit urgent and we will appreciate if you can help us on this..

    Thanks and Regards,
    Tapas

  • Hi Tapas,

    Bertrand's personal blog is not the place to ask questions like these. Although Bertrand's share in Orchard is big, it is not the product of one man. There's a whole community behind Orchard who can give you advise. Do yourself a favor and post a message on https://orchard.codeplex.com/discussions!

Comments have been disabled for this content.