← #14 Override the behavior of instanceof

Trim the spaces before parsing a number

Topics: JavaScript

Both Number() and parseInt accept the spaces in input. But be aware that you could get different result when passing a value with spaces as following:

parseInt('   5   ');    // 5
parseInt('12 345'); // 12, not 12345

To avoid the similar situations, you should remove all spaces before parsing:

parseInt(value.replace(/\s+/g, ''), 10);
Fix a typo or suggest an improvement
#16 Prevent the default behavior with jQuery event handler