The simplest way would be to use the native Number function:

var x = Number("1000")

If that doesn't work for you, then there are the parseInt, unary plus, parseFloat with floor, and Math.round methods.

parseInt()

var x = parseInt("1000", 10); // You want to use radix 10
    // So you get a decimal number even with a leading 0 and an old browser ([IE8, Firefox 20, Chrome 22 and older][1])

Unary plus

If your string is already in the form of an integer:

var x = +"1000";

floor()

If your string is or might be a float and you want an integer:

var x = Math.floor("1000.01"); // floor() automatically converts string to number

Or, if you're going to be using Math.floor several times:

var floor = Math.floor;
var x = floor("1000.01");

parseFloat()

If you're the type who forgets to put the radix in when you call parseInt, you can use parseFloat and round it however you like. Here I use floor.

var floor = Math.floor;
var x = floor(parseFloat("1000.01"));

round()

Interestingly, Math.round (like Math.floor) will do a string to number conversion, so if you want the number rounded (or if you have an integer in the string), this is a great way, maybe my favorite:

var round = Math.round;
var x = round("1000"); // Equivalent to round("1000", 0)
Answer from Nosredna on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › parseInt
parseInt() - JavaScript | MDN
The parseInt function converts its first argument to a string, parses that string, then returns an integer or NaN. If not NaN, the return value will be the integer that is the first argument taken as a number in the specified radix. (For example, a radix of 10 converts from a decimal number, ...
Top answer
1 of 16
3114

The simplest way would be to use the native Number function:

var x = Number("1000")

If that doesn't work for you, then there are the parseInt, unary plus, parseFloat with floor, and Math.round methods.

parseInt()

var x = parseInt("1000", 10); // You want to use radix 10
    // So you get a decimal number even with a leading 0 and an old browser ([IE8, Firefox 20, Chrome 22 and older][1])

Unary plus

If your string is already in the form of an integer:

var x = +"1000";

floor()

If your string is or might be a float and you want an integer:

var x = Math.floor("1000.01"); // floor() automatically converts string to number

Or, if you're going to be using Math.floor several times:

var floor = Math.floor;
var x = floor("1000.01");

parseFloat()

If you're the type who forgets to put the radix in when you call parseInt, you can use parseFloat and round it however you like. Here I use floor.

var floor = Math.floor;
var x = floor(parseFloat("1000.01"));

round()

Interestingly, Math.round (like Math.floor) will do a string to number conversion, so if you want the number rounded (or if you have an integer in the string), this is a great way, maybe my favorite:

var round = Math.round;
var x = round("1000"); // Equivalent to round("1000", 0)
2 of 16
306

Try parseInt function:

var number = parseInt("10");

But there is a problem. If you try to convert "010" using parseInt function, it detects as octal number, and will return number 8. So, you need to specify a radix (from 2 to 36). In this case base 10.

parseInt(string, radix)

Example:

var result = parseInt("010", 10) == 10; // Returns true

var result = parseInt("010") == 10; // Returns false

Note that parseInt ignores bad data after parsing anything valid.
This guid will parse as 51:

var result = parseInt('51e3daf6-b521-446a-9f5b-a1bb4d8bac36', 10) == 51; // Returns true
🌐
Priyanku's Blog
priyankuhazarika.hashnode.dev › weird-javascript-the-strange-behaviour-of-parseint
Weird Javascript: The Strange behaviour of parseInt()
April 21, 2024 - Here Javascript will first parse 0 and will stop while parsing the next character because it is a decimal, and will finally yield the result 0. It's important to note that parseInt() is primarily used to parse strings into integer values, and it may not always behave as expected with decimal numbers.
🌐
W3Schools
w3schools.com › jsref › jsref_parseint.asp
JavaScript parseInt() Method
The parseInt method parses a value as a string and returns the first integer. A radix parameter specifies the number system to use: 2 = binary, 8 = octal, 10 = decimal, 16 = hexadecimal. If radix is omitted, JavaScript assumes radix 10.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number
Number - JavaScript | MDN
Numbers are returned as-is. ... Strings are converted by parsing them as if they contain a number literal. Parsing failure results in NaN.
🌐
DEV Community
dev.to › sanchithasr › 7-ways-to-convert-a-string-to-number-in-javascript-4l
7 ways to convert a String to Number in JavaScript - DEV Community
May 15, 2024 - If you need an integer, use parseInt. If you need a floating-point number, use parseFloat. Number() is a good general-purpose option, while the unary plus operator and multiplication by 1 are convenient for quick conversions but might have ...
🌐
Spreadsheet.dev
spreadsheet.dev › number-parseint-method-javascript-apps-script
The Number object's parseInt() method
February 24, 2025 - function parseIntEx2() { // Binary (base 2) Logger.log(Number.parseInt("1010", 2)); // 10.0 (decimal equivalent of binary 1010) // Octal (base 8) Logger.log(Number.parseInt("17", 8)); // 15.0 (decimal equivalent of octal 17) // Decimal (base 10) - default Logger.log(Number.parseInt("42", 10)); // 42.0 // Hexadecimal (base 16) Logger.log(Number.parseInt("2A", 16)); // 42.0 (decimal equivalent of hex 2A) Logger.log(Number.parseInt("0x2A")); // 42.0 (0x prefix recognized as hex) // Different scenarios with hex Logger.log(Number.parseInt("0xFF", 16)); // 255.0 Logger.log(Number.parseInt("#FF", 16)); // NaN (# is not valid for parseInt) }
Find elsewhere
🌐
W3Schools
w3schools.com › js › js_number_methods.asp
JavaScript Number Methods
The number of milliseconds between 1970-01-02 and 1970-01-01 is 86400000: ... parseInt() parses a string and returns a whole number. Spaces are allowed.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › parseFloat
Number.parseFloat() - JavaScript | MDN
The Number.parseFloat() static method parses an argument and returns a floating point number. If a number cannot be parsed from the argument, it returns NaN.
🌐
Coderwall
coderwall.com › p › 5tlhmw › converting-strings-to-number-in-javascript-pitfalls
Converting Strings to Number in Javascript: Pitfalls (Example)
September 27, 2024 - Negative hexadecimal numbers are the only ones that break inside a string. Any other number should be first parsed into a String (through + "" for instance) and then parsed to a number with a unary operator or a parseInt with a radix.
🌐
Vultr Docs
docs.vultr.com › javascript › global › parseInt
JavaScript parseInt() - Parse String to Integer | Vultr Docs
September 30, 2024 - Here, parseInt() successfully extracts 456 from the string "456xyz". It discards non-numeric characters occurring after the numbers.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › parseInt
Number.parseInt() - JavaScript | MDN
The Number.parseInt() static method parses a string argument and returns an integer of the specified radix or base.
🌐
Bennadel
bennadel.com › blog › 2012-exploring-javascripts-parseint-and-parsefloat-functions.htm
Exploring Javascript's parseInt() And parseFloat() Functions
April 21, 2020 - When parsing a string into a numeric data type, the parseInt() function will start from the left and parse characters until it runs out of numeric data - the rest of the string will be ignored.
🌐
SitePoint
sitepoint.com › javascript
The advantage of using Number() instead of parseInt or parseFloat - JavaScript - SitePoint Forums | Web Development & Design Community
May 30, 2023 - Number() is both more flexible and strict. Number can interpret a value in scientific notation (“7e2” = 700), but will NaN anything that isnt actually a number format (“80px” = NaN), whereas parseInt just reads characters until it finds a non-digit and stops.
🌐
Medium
real-kevbot.medium.com › parse-integer-from-a-string-bccea39253b4
Parse Integer from a String of Written Numbers ? | by Kevin Glasgow | Medium
August 8, 2021 - Yes, I am aware that JavaScript already has a built-in parse integer function, but our parse integer function will work a little different. Normally a parse integer function is called on a string that already has a numerical value inside of it. We would call this on a string to get the numerical value within it, this value will also be a number not a string.
🌐
Artful
journal.artful.dev › what-you-risk-when-using-number-to-parse-an-integer-from-a-string-in-typescript
What you risk when using Number() to parse an integer from a string in TypeScript
May 15, 2023 - Number works with any input, but parseInt takes only string inputs. Technically, this is JavaScript with no types, and parseInt coerces any input into a string— but, that is an implementation detail. In non-technical terms, it considers any input you provide to be a string.
🌐
Better Programming
betterprogramming.pub › what-is-the-best-way-to-convert-a-string-to-a-number-in-javascript-67052a1706c6
The Best Way to Convert a String to a Number in JavaScript | by Andrew Koenig-Bautista | Better Programming
March 6, 2020 - If the character at position 0 is valid, the method moves on and carries out the same test. This goes on until parseInt() encounters an invalid number, at which point it will convert the current string into a number.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-number-parseint-method
JavaScript Number parseInt() Method - GeeksforGeeks
February 6, 2026 - It returns a number parsed up to that point where it encounters a character that is not a number in the specified radix(base). [Example 1]: Using Parsing float value · javascript · let v1 = parseInt("3.14"); console.log('Using parseInt("3.14") = '+ v1); [Example 2]: Parsing value with given radix ·
🌐
Codecademy
codecademy.com › docs › javascript › number methods › .parseint()
JavaScript | Number Methods | .parseInt() | Codecademy
May 31, 2024 - This function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems). ... Front-end engineers work closely with designers to make websites beautiful, functional, and fast.