DEV Community

Discussion on: Do you know these weird parts of JavaScript?

tchaflich profile image
Thomas C. Haflich

> 0.1 + 0.2

This is a floating point rounding error and is definitely not exclusive to JavaScript. It happens with basically any language that implements floats.

3 > 2

3 > 2 > 1

I also don't think this one is language specific. It should evaluate the same nearly anywhere that follows a left to right execution order for same-precedence operators.

A lot of the other stuff is solid "weird language spec" though. I especially like this one:

> ['1', '7', '11'].map(parseInt)
(3) [1, NaN, 3]

Took me a moment to remember what it was doing there and figure out the trick. The fact that there are two separate bits of knowledge required is especially neat.

lqj profile image
QJ Li Author

yes, you are right about these ones, I am including them to just showcase some 'weird' / interesting parts in JS. some of them are not actually weird, it is weird only when comparing with other languages.

sorry my eye catching title is kind of misleading

shreyasminocha profile image
Shreyas Minocha

I found that one interesting too. Took me some time.

I noticed that

['1', '7', '11'].map((n) => parseInt(n, 10))

works as expected. parseint is misinterpreting the second parameter somehow?

Then I saw docs for

var new_array = callback(currentValue[, index[, array]]) {
    // Return element for new_array
}[, thisArg])

If the callback accepts a second parameter, it passes the index of the element to it. parseint does accept two parameters, but of course, it doesn't expect an index in the third one.

    parseint('1', 0),
    parseint('7', 1),
    parseint('11', 2)

And sure enough, the output confirms my theory!

lqj profile image
QJ Li Author

you are close, if you take a look at the api doc, you will fully understand it.

parseInt(string, radix);
Thread Thread
shreyasminocha profile image
Shreyas Minocha

Yep! I'm aware. Pretty cool.