Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

JavaScript Dictionary

  1. Home
  2. JavaScript Dictionary
  3. parseInt() / parseFloat() / Number()

parseInt() / parseFloat() / Number()

Functions for converting a string to a number. Use parseInt() for integers, parseFloat() for numbers with decimals, and Number() for strict numeric conversion.

Syntax

// Converts a string to an integer.
var num = parseInt(string);
var num = parseInt(string, radix);

// Converts a string to a floating-point number.
var num = parseFloat(string);

// Converts a string to a number.
var num = Number(string);

Functions

FunctionDescription
parseInt(string)Converts the leading portion of a string that can be interpreted as an integer. You can specify the radix as the second argument.
parseInt(string, radix)Converts the string using the specified radix. Use 10 for decimal, 16 for hexadecimal, and 2 for binary.
parseFloat(string)Converts the leading portion of a string that can be interpreted as a number, including a decimal point.
Number(string)Strictly converts the entire string to a number. Returns NaN if the string contains any characters that cannot be interpreted as a number.

Sample Code

// parseInt() converts the leading part of a string that reads as an integer.
console.log(parseInt("42px")); // Outputs 42.
console.log(parseInt("3.14")); // Outputs 3. The decimal part is truncated.
console.log(parseInt("abc")); // Outputs NaN. Cannot be converted to a number.

// Convert using a specified radix.
console.log(parseInt("ff", 16)); // Outputs 255. Interpreted as hexadecimal.
console.log(parseInt("1010", 2)); // Outputs 10. Interpreted as binary.

// parseFloat() converts strings that include a decimal point.
console.log(parseFloat("3.14")); // Outputs 3.14.
console.log(parseFloat("100.5px")); // Outputs 100.5.

// Number() strictly converts the entire string.
console.log(Number("42")); // Outputs 42.
console.log(Number("3.14")); // Outputs 3.14.
console.log(Number("42px")); // Outputs NaN. The whole string is not a valid number.
console.log(Number("")); // Outputs 0. An empty string becomes 0.
console.log(Number(true)); // Outputs 1. true is converted to 1.

Overview

There are three main ways to convert a string to a number in JavaScript. parseInt() converts only the leading portion of the string that reads as an integer, making it useful for extracting the numeric part from CSS values like "42px". If you omit the second argument (the radix), it is usually interpreted as decimal, but specifying the radix explicitly is recommended to avoid ambiguity.

parseFloat() works similarly to parseInt() in that it converts only the leading readable portion, but it also handles numbers with decimal points correctly. Number(), on the other hand, returns NaN if the entire string is not a valid number, making it well-suited for strict conversions such as validating user input.

You can also use the unary plus operator, as in +"42", to get the same result as Number(). However, this makes the intent of your code less clear, so using Number() explicitly is recommended.

Browser Compatibility

Chrome Chrome
49+
Supported in all versions
Firefox Firefox
57+
Supported in all versions
Safari Safari
18+
Supported in all versions
Edge Edge
80+
11 or earlier ×
IE IE
11+
2 or earlier ×
Opera Opera
48+
2 or earlier ×
iOS Safari iOS Safari
18+
Supported in all versions
Android Browser Android Browser
37+
4 or earlier ×
Chrome Android Chrome Android
36+
17 or earlier ×
Firefox Android Firefox Android
79+
3 or earlier ×

If you find any errors or copyright issues, please .