How to Convert String to Int in JavaScript

Converting a string to an integer in JavaScript is a common task. This guide will show you how to do it using different methods.

What is a String?

A string is a sequence of characters. For example, “123” is a string that represents the number 123.

What is an Integer?

An integer is a whole number, such as 123, without any decimal points.

Methods to Convert String to Int in JavaScript

1. Using parseInt()

The parseInt() function converts a string to an integer.

Example: Convert String to Int using parseInt()

let str = "123";
let num = parseInt(str);
console.log(num); // Output: 123

2. Using Number()

The Number() function converts a string to a number.

Example: Convert String to Int using Number()

let str = "123";
let num = Number(str);
console.log(num); // Output: 123

3. Using Unary Plus Operator

The unary plus operator converts a string to a number.

Example: Convert String to Int using Unary Plus

let str = "123";
let num = +str;
console.log(num); // Output: 123

Handling Different Scenarios

1. String with Leading Zeros

If the string has leading zeros, all methods will ignore them.

Example: String with Leading Zeros

let str = "00123";
console.log(parseInt(str)); // Output: 123
console.log(Number(str)); // Output: 123
console.log(+str); // Output: 123

2. String with Non-Numeric Characters

If the string contains non-numeric characters, the result will be NaN (Not a Number).

Example: String with Non-Numeric Characters

let str = "123abc";
console.log(parseInt(str)); // Output: 123
console.log(Number(str)); // Output: NaN
console.log(+str); // Output: NaN

3. Empty String

Converting an empty string will result in NaN.

Example: Empty String

let str = "";
console.log(parseInt(str)); // Output: NaN
console.log(Number(str)); // Output: NaN
console.log(+str); // Output: NaN

Error Handling

It’s important to handle cases where the conversion might fail. You can use isNaN() to check if the result is a number.

Example: Check if Conversion is Successful

let str = "123abc";
let num = parseInt(str);
if (!isNaN(num)) {
  console.log(num);
} else {
  console.log("Invalid number");
}
// Output: 123

str = "abc";
num = Number(str);
if (!isNaN(num)) {
  console.log(num);
} else {
  console.log("Invalid number");
}
// Output: Invalid number

Frequently Asked Questions

Q1: What is the difference between parseInt() and Number()?

  • parseInt() only converts the string to an integer and ignores any decimal points.
  • Number() converts the string to a number, which can be an integer or a floating-point number.

Q2: Why do we need multiple methods to convert string to int?

Different methods are useful in different scenarios. For example, parseInt() is useful when you want to convert a string that represents a number in a different base (e.g., binary, hexadecimal).

Q3: Can we convert a string that represents a floating-point number to an integer?

Yes, but the result will be the integer part of the number. For example, parseInt("123.45") will return 123.

Q4: How to handle strings with leading or trailing whitespace?

The parseInt() and Number() functions will ignore leading and trailing whitespace. For example, parseInt(" 123 ") will return 123.

Q5: What if the string is null or undefined?

Converting null to a number will result in 0, and converting undefined will result in NaN.

Example: Convert Null and Undefined

console.log(Number(null)); // Output: 0
console.log(Number(undefined)); // Output: NaN

Conclusion

There are multiple ways to convert a string to an integer in JavaScript. The choice of method depends on the specific requirements of your task. Always handle cases where the conversion might fail to avoid unexpected behavior in your code.

Index
Scroll to Top