👋 Hello! I'm Alphonsio the robot. Ask me a question, I'll try to answer.

In JavaScript, how to convert a string to an integer?

The simplest way to convert a string into an integer in JavaScript is to use the native Number() function:

const string='123.4';
var x=Number(string);

// Expected output: "number 123.4"
console.log (typeof x, x);
The second option is to use the parseInt() function:

const string='123';
var x=parseInt(string);
parseInt() only works for integers.

More