parseInt("123qwe")
returns 123
Number("123qwe")
returns NaN
In other words parseInt() parses up to the first non-digit and returns whatever it had parsed. Number() wants to convert the entire string into a number, which can also be a float BTW.
EDIT #1: Lucero commented about the radix that can be used along with parseInt(). As far as that is concerned, please see THE DOCTOR's answer below (I'm not going to copy that here, the doc shall have a fair share of the fame...).
EDIT #2: Regarding use cases: That's somewhat written between the lines already. Use Number() in cases where you indirectly want to check if the given string completely represents a numeric value, float or integer. parseInt()/parseFloat() aren't that strict as they just parse along and stop when the numeric value stops (radix!), which makes it useful when you need a numeric value at the front "in case there is one" (note that parseInt("hui") also returns NaN). And the biggest difference is the use of radix that Number() doesn't know of and parseInt() may indirectly guess from the given string (that can cause weird results sometimes).
Basic JavaScript - Use the parseInt Function
javascript - Calling parseInt with a string which represents a value larger than Number.MAX_SAFE_INTEGER - Stack Overflow
How can I convert a string to an integer in JavaScript? - Stack Overflow
[AskJS] Explaining parseInt in JavaScript with Scientific Notation
Videos
parseInt("123qwe")
returns 123
Number("123qwe")
returns NaN
In other words parseInt() parses up to the first non-digit and returns whatever it had parsed. Number() wants to convert the entire string into a number, which can also be a float BTW.
EDIT #1: Lucero commented about the radix that can be used along with parseInt(). As far as that is concerned, please see THE DOCTOR's answer below (I'm not going to copy that here, the doc shall have a fair share of the fame...).
EDIT #2: Regarding use cases: That's somewhat written between the lines already. Use Number() in cases where you indirectly want to check if the given string completely represents a numeric value, float or integer. parseInt()/parseFloat() aren't that strict as they just parse along and stop when the numeric value stops (radix!), which makes it useful when you need a numeric value at the front "in case there is one" (note that parseInt("hui") also returns NaN). And the biggest difference is the use of radix that Number() doesn't know of and parseInt() may indirectly guess from the given string (that can cause weird results sometimes).
The first one takes two parameters:
parseInt(string, radix)
The radix parameter is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number.
If the radix parameter is omitted, JavaScript assumes the following:
- If the string begins with "0x", the
radix is 16 (hexadecimal) - If the string begins with "0", the
radix is 8 (octal). This feature
is deprecated - If the string begins with any other value, the radix is 10 (decimal)
The other function you mentioned takes only one parameter:
Number(object)
The Number() function converts the object argument to a number that represents the object's value.
If the value cannot be converted to a legal number, NaN is returned.
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)
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
Hey everyone in r/javascript,
I recently came across a tweet questioning how JavaScript's parseInt function behaves with numbers like 0.5, 0.05, 0.005, etc., and why it returns 5 for 0.0000005.
Here's a concise explanation: JavaScript represents numbers smaller than 1e-6 in scientific notation. For example, 0.0000005 becomes '5e-7'. When parseInt is used on this string, it reads up to the first non-numeric character, which in this case is 'e'. Therefore, parseInt('5e-7') results in 5.
This behaviour is a mix of how JavaScript handles number-to-string conversion for small numbers and the workings of parseInt. Thought this might be an interesting share for those puzzled by JavaScript's quirky nature!
here is an image for more explanation
https://twitter.com/ibrahimwithi/status/1751563262418674151/photo/1
function roughScale(x, base) {
const parsed = Number.parseInt(x, base);
if (Number.isNaN(parsed)) { return 0; }
return parsed * 100;
}
console.log(roughScale(' 0xF', 16)); // expected output: 1500
console.log(roughScale('321', 2)); // expected output: 0
I understand that parseInt is a method used to parse a string and return an integer. But I'm kind of confused how the function gets the expected output results.
Wouldn't the returned parsed values be:
(0, 16) * 100?
and
(321,16) * 100?
So why are the expected output 1500 and 0?
Also what does 'Number.NaN(parsed)' mean does it mean if the string parsed is not a number it would return 0?