Question #61   Submitted by Answiki on 06/06/2020 at 04:20:49 PM UTC

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

Answer   Submitted by Answiki on 06/06/2020 at 04:17:17 PM UTC

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.

5 events in history
Question by Answiki 06/06/2020 at 04:20:49 PM
In JS, how to convert a string to an integer?
Question by Answiki 06/06/2020 at 04:20:43 PM
In JavaScript, how to convert a string to an integer?
Question by Answiki 06/06/2020 at 04:17:40 PM
How to convert a JS string to an integer?
Answer by Answiki on 06/06/2020 at 04:17:17 PM

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.

Answer by Answiki on 06/06/2020 at 04:16:26 PM

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);

# ID Query URL Count

Icons proudly provided by Friconix.