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 Web Docs
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, ...
🌐
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.
Top answer
1 of 16
3115

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
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › parseFloat
Number.parseFloat() - JavaScript - MDN Web Docs
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.
🌐
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 ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number
Number - JavaScript - MDN Web Docs - Mozilla
Determine whether the passed value is a safe integer (number between -(253 - 1) and 253 - 1). ... This is the same as the global parseFloat() function.
🌐
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.
Find elsewhere
🌐
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.
🌐
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 ·
🌐
TechOnTheNet
techonthenet.com › js › number_parseint.php
JavaScript: Number parseInt() method
This JavaScript tutorial explains how to use the Number method called parseInt() with syntax and examples. In JavaScript, parseInt() is a Number method that is used to parse a string and return its value as an integer number.
🌐
W3Schools
w3schools.com › jsref › jsref_number_parseint.asp
JavaScript Number.parseInt()
The Number.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.
🌐
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.
🌐
W3Schools
w3schools.com › jsref_parseint.asp
JavaScript parseInt() Function
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS ... var a = parseInt("10") + "<br>"; var b = parseInt("10.00") + "<br>"; var c = parseInt("10.33") + "<br>"; var d = parseInt("34 45 66") + "<br>"; var e = parseInt(" 60 ") + "<br>"; var f = parseInt("40 years") + "<br>"; var g = parseInt("He was 40") + "<br>"; var h = parseInt("10", 10)+ "<br>"; var i = parseInt("010")+ "<br>"; var j = parseInt("10", 8)+ "<br>"; var k = parseInt("0x10")+ "<br>"; var l = parseInt("10", 16)+ "<br>"; var n = a + b + c + d + e + f + g + "<br>" + h + i + j + k +l; Try it Yourself »
🌐
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.
🌐
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.
🌐
Medium
medium.com › @priyanshitaneja › understanding-javascript-number-and-parseint-a-comprehensive-guide-390020d40d2d
Understanding JavaScript Number() vs parseInt(): A Comprehensive Guide | by Priyanshi Taneja | Medium
July 10, 2024 - This means it can parse a string containing numbers followed by non-numeric characters, returning the parsed number up to that point but returning NaN in case it encounters booleans, null objects, or an undefined data type.
🌐
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.