JavaScript traps · JavaScript

JavaScript: parseInt(0.0000005) = 5?!PRO

Watch the video

parseInt turns the number into text first: "5e-7". Use Math.trunc() for numbers.

// parseInt(0.0000005) = 5 ?! — parseInt turns the number into TEXT first: "5e-7".
console.log(String(0.0000005));       // "5e-7"
console.log(parseInt(0.0000005));     // 5 ?!  (reads "5", stops at "e")
console.log(Math.trunc(0.0000005));   // 0 ✓  (use Math for numbers)
console.log(parseInt("42px"));        // 42 — parseInt is for TEXT
Open code JS5 in Pocket Compiler Download the Pocket Compiler app — free

No app yet? Install it, then open the app and type the code JS5 in Code Library.