Converting a String to an Integer in JavaScript
JavaScript provides multiple ways to convert a string ("123"
) into an integer (123
). Here are the most commonly used methods:
โ
1. Using parseInt()
(Recommended for Integers)
The parseInt()
function extracts and converts the numeric part of a string into an integer.
let str = "123";
let num = parseInt(str, 10); // The second argument (10) specifies base-10 conversion.
console.log(num); // Output: 123
console.log(typeof num); // Output: "number"
๐น Why Use parseInt()
?
โ Can handle numbers inside a string (e.g., "123abc"
โ 123
).
โ Allows specifying radix (base) for different number systems.
โ Ignores decimals (e.g., "123.45"
โ 123
).
โ
2. Using Number()
(Best for Clean Numbers)
The Number()
function converts the entire string into a number.
let str = "123";
let num = Number(str);
console.log(num); // Output: 123
console.log(typeof num); // Output: "number"
๐น Why Use Number()
?
โ Works for both integers and decimals ("123.45"
โ 123.45
).
โ Returns NaN
(Not a Number) for invalid input ("123abc"
โ NaN
).
โ
3. Using Unary +
Operator (Fastest & Concise)
The +
(unary plus) operator is the shortest way to convert a string to a number.
let str = "123";
let num = +str;
console.log(num); // Output: 123
console.log(typeof num); // Output: "number"
๐น Why Use +
?
โ Fastest method (optimized by JavaScript engines).
โ Works for both integers & decimals.
โ Can return NaN
for non-numeric values (+"123abc"
โ NaN
).
โ
4. Using Math.floor()
, Math.ceil()
, or Math.round()
If the string contains a decimal number and you need an integer, use:
let str = "123.99";
console.log(Math.floor(str)); // Output: 123 (Rounds down)
console.log(Math.ceil(str)); // Output: 124 (Rounds up)
console.log(Math.round(str)); // Output: 124 (Rounds to nearest)
๐น Why Use These?
โ Useful for rounding decimal numbers before conversion.
โ Slower than parseInt()
for integer-only strings.
โ
5. Using ~~
(Bitwise NOT Operator)
The ~~
operator truncates a floating-point number to an integer.
let str = "123.45";
let num = ~~str;
console.log(num); // Output: 123
๐น Why Use ~~
?
โ Shortest method to remove decimals (acts like Math.floor()
).
โ Doesn’t work on very large numbers (only works within 32-bit integer range).
๐น Handling Edge Cases
Input | parseInt() |
Number() |
+str |
---|---|---|---|
"123" |
โ
123 |
โ
123 |
โ
123 |
"123.45" |
โ
123 (truncated) |
โ
123.45 |
โ
123.45 |
"123abc" |
โ
123 (stops at non-numeric) |
โ NaN |
โ NaN |
"abc123" |
โ NaN |
โ NaN |
โ NaN |
โ Best Practices & Recommendations
- โ
Use
parseInt(str, 10)
if you only need an integer. - โ
Use
Number(str)
or+str
if you want both integers & decimals. - โ
Use
Math.floor()
if you need integer rounding. - โ Avoid
parseInt()
for decimal numbers ("123.45"
โ123
).
Related posts:
- How to convert a string to an integer in JavaScript?
- How do I convert a string to an integer in JavaScript?
- How to Convert a String to an Integer in Java Script?
- Convert String To Integer Type In Go?
- Convert integer to string in PostgreSQL
- What Is the Best Way to Coerce a Value to a String in JavaScript?
Leave a comment