JavaScript provides several ways to convert a numeric string into an integer without using the parseInt() function. These approaches use type coercion, the Number() function, unary +, and mathematical methods.
- Use type coercion or the unary + operator to convert a numeric string into a number.
- Use Number() to explicitly convert a string to a numeric value.
- Use Math.floor(), Math.ceil(), or Math.round() when the string may contain a decimal value and rounding is required.
Approach 1: Using Type Coercion
Multiplying a numeric string by 1 automatically converts the string into a number. If the string is not a valid numeric value, the result is NaN.
Example: In this example, we multiply the strings by 1 and check the resulting data types
function convertStoI() {
let a = "100";
let b = a * 1;
console.log(typeof b);
let d = "3 11 43" * 1;
console.log(typeof d);
}
convertStoI();
Output
number number
Note: The second value is actually NaN, whose type is still "number" in JavaScript.
Approach 2: Using the Number() Function
The Number() function explicitly converts a value into a number. For a valid integer string, it returns the corresponding numeric value; for an invalid numeric string, it returns NaN.
Example: In this example, we use Number() to convert the string "100" into a number.
function convertStoI() {
const a = "100";
const b = Number(a);
console.log(b);
console.log(typeof b);
const d = Number("3 11 43");
console.log(d);
console.log(typeof d);
}
convertStoI();
Output
100 number NaN number
Approach 3: Using the Unary + Operator
The unary + operator can be placed before a string to convert a numeric string into a number. It is a concise alternative to Number().
Example: In this example, we use the unary + operator to convert the strings.
function convertStoI() {
let a = "100";
let b = +a;
console.log(b);
console.log(typeof b);
let d = +"3 11 43";
console.log(d);
console.log(typeof d);
}
convertStoI();
Output
100 number NaN number
Approach 4: Using Math.floor() Method
The Math.floor() method rounds a numeric value down to the nearest integer. Since JavaScript automatically converts a numeric string to a number when passed to Math.floor(), it can be used to convert and round the value.
Example: In this example, Math.floor() converts the numeric string "100.8" into the integer 100.
function convertStoI() {
let a = "100.8";
let b = Math.floor(a);
console.log(b);
console.log(typeof b);
}
convertStoI();
Output
100 number
Approach 5: Using Math.ceil() Method
The Math.ceil() method rounds a numeric value upward to the nearest integer. It can also convert a numeric string into a number before performing the rounding operation.
Example: In this example, Math.ceil() converts "100.2" to 101.
function convertStoI() {
let a = "100.2";
let b = Math.ceil(a);
console.log(b);
console.log(typeof b);
}
convertStoI();
Output
101 number
Approach 6: Using Math.round() Method
The Math.round() method rounds a numeric value to the nearest integer. It can be used when the numeric string contains a decimal value and the nearest integer is required.
Example: In this example, Math.round() converts "100.4" to the nearest integer.
function convertStoI() {
let a = "100.4";
let b = Math.round(a);
console.log(b);
console.log(typeof b);
}
convertStoI();
Output
100 number