To convert a string to an integer in JavaScript, use parseInt() with a radix of 10 for reliable results:

parseInt("123", 10); // Returns 123

Always specify the radix (e.g., 10 for decimal) to avoid unexpected behavior, especially with strings starting with 0 (e.g., "010").

Alternative methods include:

  • Number(): Converts the entire string to a number. Returns NaN if the string is not a valid number.

    Number("123"); // Returns 123
  • Unary plus (+): A concise shorthand that works for both integers and floats.

    +"123"; // Returns 123
  • Math.trunc(): Useful when you want to remove decimal places after converting to a number.

    Math.trunc("123.456"); // Returns 123

For handling non-numeric input, check for NaN using isNaN():

const value = "abc";
if (!isNaN(parseInt(value, 10))) {
  console.log("Valid integer");
} else {
  console.log("Invalid input");
}

Choose parseInt() when you want to extract the first integer from a mixed string (e.g., "456abc"456). Use Number() or the unary plus operator for general type conversion.

🌐
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
March 6, 2020 - The parse methods, as the name suggests, attempt to parse through a string piece-by-piece as they convert it, Number() attempts to convert the entire string argument into a number, all at once. const str = '123abc'; Number(str); // NaN (attempts to convert entire string, cannot) parseInt(s); // 123 (stops as soon as it hits `a`) Number() is the JavaScript Number constructor called as a function, which performs a type conversion.
🌐
upGrad
upgrad.com › home › tutorials › software & tech › convert string to int in javascript
Convert String to int in JavaScript
February 3, 2025 - Let us learn how to convert string to int in javascript with different methods and why it is important to do so in this tutorial.
Discussions

What is the method for transforming a string into an integer using JavaScript?
I am looking for a way to change a string representation of a number into an actual integer in JavaScript. What techniques or functions can I use to achieve this? More on community.latenode.com
🌐 community.latenode.com
0
October 1, 2024
What's the best way to convert a number to a string in JavaScript? - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... What's the "best" way to convert a number to a string (in terms of speed advantage, clarity advantage, memory advantage, etc) ? More on stackoverflow.com
🌐 stackoverflow.com
How can I convert a string to an integer in JavaScript? - Stack Overflow
So if you have noticed parseInt ... suitable if you want to retain the values after decimals. Use parseInt if and only if you are sure that you want the integer value. ... The question was "How do I convert a String into an integer in javascript" 2015-09-02T14:08:16.3... More on stackoverflow.com
🌐 stackoverflow.com
The unary + operator is a shortcut to convert a string into a number 🤩
Neat. Don't do this. More on reddit.com
🌐 r/webdev
282
999
March 31, 2018
People also ask

How to get only int value from string in JavaScript?
Use parseInt() to extract the leading integer from a string. If the string contains other characters, parseInt() will stop parsing when it encounters a non-numeric character.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › convert string to int in javascript
Convert String to int in JavaScript
How to convert string to int without using parseInt JavaScript?
You can use the number() constructor which carries out JavaScript convert string to integer (integer or float if decimals are present). You can also use the unary plus operator (+) which converts string to a number type.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › convert string to int in javascript
Convert String to int in JavaScript
How to check if string can be converted to int JavaScript?
Use the isNaN() function in combination with parseInt(). If isNaN(parseInt(yourString)) returns true, the string can't be reliably converted to an integer.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › convert string to int in javascript
Convert String to int in JavaScript
🌐
OneCompiler
onecompiler.com › questions › 3xnp7szab › -javascript-what-is-the-way-to-convert-a-string-to-an-integer
[Javascript] What is the way to convert a string to an integer? - Questions - OneCompiler
December 28, 2021 - I want to convert a string to an integer, How can I do that in JavaScript? 4 years ago by Jahaan · You can either use Number() function or parseInt() function is used to convert a string to an integer in Javascript ·
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-a-string-into-a-integer-without-using-parseint-function-in-javascript
Convert a string into a integer without using parseInt() function in JavaScript ? - GeeksforGeeks
2 weeks ago - In JavaScript, there is a simple function parseInt() to convert a string to an integer. In order to know more about the function, you can refer to this.
🌐
Oreate AI
oreateai.com › blog › mastering-string-to-integer-conversion-in-javascript › 190420612cc4bf7e5db5180dc08ff6e6
Mastering String to Integer Conversion in JavaScript - Oreate AI Blog
December 31, 2025 - This method works seamlessly for straightforward cases but returns NaN (Not-a-Number) if the conversion fails—like trying to convert non-numeric strings such as 'Hello'. Another popular approach involves using parseInt(), which specifically parses strings and extracts integers:
🌐
Fonzi AI
fonzi.ai › blog › convert-string-to-integer
How to Convert a String to an Integer (in Python, JavaScript ...
August 12, 2025 - Join Fonzi to get multiple salary-backed offers from top tech companies. One application. Curated opportunities and concierge support.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › parseInt
Number.parseInt() - JavaScript | MDN
July 10, 2025 - The Number.parseInt() static method parses a string argument and returns an integer of the specified radix or base. function roughScale(x, base) { const parsed = Number.parseInt(x, base); if (Number.isNaN(parsed)) { return 0; } return parsed * 100; } console.log(roughScale(" 0xF", 16)); // ...
Find elsewhere
🌐
Latenode
community.latenode.com › other questions › javascript
What is the method for transforming a string into an integer using JavaScript? - JavaScript - Latenode Official Community
October 1, 2024 - I am looking for a way to change a string representation of a number into an actual integer in JavaScript. What techniques or functions can I use to achieve this?
🌐
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.
🌐
W3Schools
w3schools.com › js › js_type_conversion.asp
JavaScript Type Conversions
The global method Number() converts a variable (or a value) into a number. A numeric string (like "3.14") converts to a number (like 3.14).
🌐
W3Schools
w3schools.com › jsref › jsref_tostring_number.asp
JavaScript Number toString() Method
The toString() method is used by JavaScript when an object needs to be displayed as a text (like in HTML), or when an object needs to be used as a string.
🌐
Built In
builtin.com › articles › javascript-string-to-a-number
How to Convert a JavaScript String to Number | Built In
I prefer to use the Number() method because it’s easier for developers to read the code, and it’s quite intuitive to realize that it’s converting a string to Number. Performance chart rating each method to convert a javascript string to a number.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › toString
Number.prototype.toString() - JavaScript | MDN
The toString() method of Number values returns a string representing this number value. function hexColor(c) { if (c < 256) { return Math.abs(c).toString(16); } return 0; } console.log(hexColor(233)); // Expected output: "e9" console.log(hexColor("11")); // Expected output: "b" ... An integer ...
🌐
GreatFrontEnd
greatfrontend.com › questions › quiz › how-do-you-convert-a-string-to-a-number-in-javascript
How do you convert a string to a number in JavaScript? | Quiz Interview Questions with Solutions
September 5, 2021 - The parseInt() function parses a string and returns an integer. It can also take a second argument, the radix (base) of the numeral system to be used.
🌐
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, ...
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Data types
For example, a variable can at one moment be a string and then store a number: // no error let message = "hello"; message = 123456; Programming languages that allow such things, such as JavaScript, are called “dynamically typed”, meaning that there exist data types, but variables are not bound to any of them. ... The number type represents both integer and floating point numbers.
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
🌐
egghead.io
egghead.io › lessons › typescript-parse-a-string-to-an-integer
Parse a string to an integer | egghead.io
For example, the string 123 to its integer value 123. We start by creating our atoi function that takes a string and returns a number. [00:21] JavaScript has a function called parseInt that can do this integer parsing for us.
Published   July 27, 2017