January 2020
Intermediate to advanced
470 pages
11h 13m
English
Working with the map is usually far safer and simpler than looping by hand, but some edge cases may trip you up. Say you received an array of strings representing numeric values, and you wanted to parse them into actual numbers. Can you explain the following results?
["123.45", "67.8", "90"].map(parseFloat);// [123.45, 67.8, 90]["123.45", "-67.8", "90"].map(parseInt);// [123, NaN, NaN]
Let's analyze the results. When we used parseFloat() to get floating-point results, everything was okay; however, when we wanted to truncate the results to integer values, then the output was really awry, and weird NaN values appeared. What happened?
The answer lies in a problem with tacit programming. (We have already seen some uses ...
Read now
Unlock full access