How do you convert a string to an integer in JavaScript?

How do you convert a string to an integer in JavaScript?

a) parseInt()
b) parseFloat()
c) Number()
d) toString()

Answer:

a) parseInt()

Explanation:

The parseInt() function converts a string to an integer by parsing the string and returning the first integer value found. If the string does not contain a number, NaN (Not-a-Number) is returned.

Example:


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

Reference:

JavaScript MCQ (Multiple-Choice Questions)

Scroll to Top