Which function converts a string to an integer in JavaScript?
Answer
parseInt()
Answer
parseInt()
parseInt() converts text into an integer in JavaScript by reading a sequence of digits and returning a whole-number value. For example, parseInt("42", 10) returns 42.
The optional second argument is the radix, or number base, and specifying it explicitly is good practice. A radix of 10 means decimal notation; other values support bases such as hexadecimal or binary. parseInt() truncates at the point where the input no longer fits the chosen integer format, so parseInt("12px", 10) returns 12.
parseInt() is often confused with Number() and parseFloat(). Number() attempts to convert the entire value to a number, while parseFloat() preserves a fractional part. Thus parseInt("3.9", 10) returns 3, whereas parseFloat("3.9") returns 3.9. If no valid integer begins the input, parseInt() returns NaN.
Source: Wikipedia · fact-checked Aug. 2026