JavaScript. Numeric data type Number. Conversion functions between numeric type and other types

Numeric data type Number. Conversion functions between numeric type and other types. Functions Number(), parseInt(), parseFloat()


Search other resources:

1. Features of the numeric data type Number

JavaScript uses the Number data type to work with numbers. When you assign a numeric value to a variable, that variable is of type Number (the variable is a Number object). This type encodes numbers in 64-bit floating point format.

Values of the Number type can vary within

± 5·10-324 ..   ± 1.7976931348623157·10308

The Number type can represent both an integer and a floating point number. In JavaScript, there is no separation between integers and real numbers (types).

Any number in JavaScript is taken in a wrapper – the Number object. The Number object has a number of constants, functions, and properties used to process and represent numbers.


2. Classification of functions of the Number object

The following are the main functions that you can use when working with the Number object.

Functions that determine the current state of the Number object:

  • Number.isFinite();
  • Number.isInteger();
  • Number.isNan();
  • Number.isSafeInteger();

Functions for mutual conversion between the Number type and other types:

  • Number.Number() or Number();
  • Number.parseFloat() or Float();
  • Number.parseInt() or parseInt().

Functions for representing Number objects in different notations or formats:

  • Number.prototype.toExponential();
  • Number.prototype.toFixed();
  • Number.prototype.toLocaleString();
  • Number.prototype.toLocaleString();
  • Number.prototype.toPrecision();
  • Number.prototype.toString();
  • Number.prototype.valueOf().


3. List of properties of the Number object

The properties defined for the Number object are listed below:

  • Number.EPSILON;
  • Number.MAX_VALUE;
  • Number.MIN_VALUE;
  • Number.Nan;


4. Examples of using numeric type values


// Declaring variables of type Number
var NumMonths = 12
var NumDays = 7
var population = 4900000000

// Using number variables in arithmetic expressions
var a = 25
var b = a*5 // b = 125

var c = a+b // c = 150

// Display the value of c in the window
alert("c = " + c)


5. Functions (methods) for converting numeric types

The conversion between JavaScript numeric types and other types is done using functions:

  • Number();
  • parseInt();
  • parseFloat().

You can access functions in a program in one of two ways:

  • with the type name Number, for example Number.parseInt();
  • without specifying a type name, such as parseInt().


5.1. Function (Method) Number(). Get number of type Number (get Number object)

The Number() function is used to get a number from a string or boolean value. This number can be used in calculations.

In the most general case, the use of the Number() function is

numberValue = Number(anyValue)


  • anyValue is a random value type that is converted to a number of type Number;
  • numberValue is the result of type Number.

With different values of anyValue, the resulting value can be as follows:

  • if anyValue is of type String that converts to a number (“123”), then the result will be that number of type Number;
  • if anyValue is of type String, which is not convertible to a number (abcd), then the result will be NaN (Not a Number);
  • if anyValue is of type boolean and anyValue=true, then the result will be the value 1;
  • if anyValue is of type boolean and anyValue=false, the result is 0.


// Method Number() - get a number
var x = Number("55") // x = 55
x = Number("abcd")   // x = Nan
x = Number(true)     // x = 1
x = Number(false)   // x = 0


5.2. Function parseInt(). Get the integer part of a fractional number

Since all numbers of type Number are floating-point numbers, the parseInt() function is used to get an integer. This function allows you to get the integer part of a number by discarding the fractional value (the value after the decimal point).

The function usage can be as follows

resValue = parseInt(anyValue)


  • resValue – the resulting value;
  • anyValue is a value of an arbitrary type that needs to be converted to an integer of type Number.

If it is impossible to form a number from the value of anyValue (a string of type “abcd”, a value of false, etc.), then the function returns NaN (Not a Number).


// Method Number() - get a number
var x = 2.885
var res = parseInt(x) // res = 2

res = parseInt("4.13") // res = 4
res = parseInt("jklmn") // res = Nan
res = parseInt(true)   // res = Nan
res = parseInt(10.88*23.12+10.33) // res = 261


5.3. Function parseFloat(). Set number as floating point number

The parseFloat() function tells you to get a floating point number. This function can also be used to convert a string to a number. In the most general case, the use of the function has the form

floatValue = parseFloat(anyValue)


  • anyValue is the value to be set as a floating point number. The value of anyValue can be not only of type String.

If it is not possible to create a number from the value of anyValue (the string “jprst”, the value true, etc.), the method returns NaN.


// Function parseFloat() - get number from string
var res = parseFloat("7.782") // res = 7.782
res = parseFloat("2239") // res = 2239
res = parseFloat(false)   // res = NaN
res = parseFloat(22.3309) // res = 22.3309
res = parseFloat("jprst") // res = NaN


Related topics