Convert Number to Integer in JavaScript

Convert Number to Integer in JavaScript

To convert a number to an integer in JavaScript you can use the parseInt() function. Below is an example:

JavaScript: Number to Integer Examples:

val = parseInt(10.22);
console.log(val); // output 10
val = parseInt(33.23232);
console.log(val); // output 33

a = 89.90;
b = parseInt(a);
console.log(b); // output 89

What is parseInt() in JavaScript?

The function parseInt() parses a string argument and returns an integer of the chosen radix (the base in mathematical numeral systems).

Syntax

parseInt(string)
parseInt(string, radix)

Parameters

string

A string that begins with an integer. In this parameter, the leading whitespace is ignored. radix is Optional.

Return value

When an integer is parsed from the given string, it returns NaN.

  • The radix as a 32-bit integer is less than 2 or greater than 36, or
  • The first character that is not whitespace cannot be turned to a number.

See also: