JavaScript is broken!

Ketan Jakhar
5 min read8 hours ago

As a backend developer with over two years of professional experience working with Node.js and TypeScript, I’ve spent countless hours debugging and deciphering JavaScript’s sometimes baffling behaviours. Many of these quirks stem from fundamental JavaScript concepts that are easy to overlook or forget, even for seasoned developers.

1. The Misleading parseInt Function

parseInt can produce unexpected results if the radix (base) isn't specified:

console.log(parseInt('08')); // Output: 8
console.log(parseInt('09')); // Output: 9 in modern browsers, NaN in older ones

In older environments, strings starting with ‘0’ are treated as octal numbers.

Best Practice:

Always specify the radix:

console.log(parseInt('09', 10)); // Output: 9

⭐Bonus: Radix Confusion with map()

Why ['2', '7', '12'].map(parseInt) Returns [2, NaN, 1]?

This behavior occurs because map() passes three arguments to the callback function: the element, the index, and the array itself. parseInt takes two arguments: the string to convert and the radix (the base for the number). So, when you use map(parseInt), the index is passed as the second argument to parseInt, causing…

--

--

Ketan Jakhar

everything backend | NodeJS | TypeScript | Blockchain