Converting Strings to Numbers

new Number("16");   //returns 16 new Number("0x20"); //returns 32 new Number("32.8"); //returns 32.8

To convert numerical-based strings into a number, create a new number object. JavaScript automatically detects whether the string is a number (even hex number with 0x## formatting) and create a new number object. If the string is not a valid number format, a NaN object is returned.

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.