Two families, with different philosophies:
Conversion: Number(x) and unary +x. Both apply the language's ToNumber conversion to the whole value, so they behave the same on strings (MDN: unary plus uses the same steps as number coercion). The string must be entirely a valid numeric literal, apart from surrounding whitespace.
Number('42px')isNaN. Any trailing junk makes it fail.Number('')andNumber(' ')are0, notNaN. This is the classic trap with empty form fields.- Decimal, exponent and
0x/0b/0oprefixed forms work:Number('0x1f')is31. Numeric separators don't:Number('1_000')isNaN. - Non-strings convert too:
null→0,undefined→NaN,true→1,[]→0. - Difference:
Number(1n)returns1, while+1nthrowsTypeError.
Parsing: parseInt(string, radix) and parseFloat(string). Both coerce the argument to a string, skip leading whitespace, and read the longest valid prefix. Anything after it is ignored; if there is no valid prefix the result is NaN.
parseInt('42px', 10)is42;parseInt('')isNaN.parseIntreturns integers only ('3.9'→3) and takes a radix from 2 to 36. Without a radix,0xmeans hexadecimal and everything else decimal. Always pass one:parseInt(s, 10).parseFloathandles decimals and exponents, accepts'Infinity', and doesn't understand hex:parseFloat('0x10')is0.
Rule of thumb: use Number() when the whole input must be a number (forms, query params, config). Use parseInt/parseFloat only when you expect trailing units such as '12px'. Neither validates for you: check with Number.isNaN, or Number.isFinite to reject infinities too.