JavaScript Numbers

JavaScript Numbers

JavaScript Numbers is a common object type in programming. This is the type of object needed when you have to work with numeric data. In this article, we will summarize the most basic and necessary knowledge of the Number object.

What is JS Numbers?

There are two data types associated with numbers in JavaScript: regular number and BigInt.

  • Numbers are normally stored as IEEE-754 floating point real numbers with double precision.
  • BigInt is a data type used to represent integers of arbitrary length. The bigInt is used in some cases where large integer representation is required.

Unlike other languages, JavaScript doesn’t treat integers and floating point differently. In JS, numbers will be represented as floating point numbers.

For example:

var a = 114; // integer
var b = 96.8; // floating point
var c = 15e2; // the exponent e

Declare numbers in JS

To declare numbers in javascript we use the syntax:

var name = value; // number
var name = new Number(value); // object

In there:

  • name is the name of the variable you want to set, but must satisfy the naming rules.
  • value is the value of that variable. It can be integer, real, exponential, or binary, decimal, and hexadecimal.

Here are some examples of how to declare numbers as integer, real, exponential, or binary, decimal, and hexadecimal values.

//Declare an integer variable
var t1 = 2;

// Declare the variable as a real number
var t2 = 4.6;

//Declare an exponential variable
var t3 = 1e6 // 1000000

//Declare an octal variable
var t4 = 0123 //83

//Declare a hexadecimal variable
var t5 = 0x1A; //26

//Declare a binary variable
var t6 = 0b11111111; //255

Infinity number

Infinity is also a Number data type and when a variable has a value of Infinity, it means it has exceeded the allowed storage level, so by default it will convert to that form. Since it is also a value, you can compare normally.

Example: Use a while loop to loop until the variable itNumber has the value Infinity.

var itNumber = 3;

while (itNumber!= Infinity) {
     itNumber = itNumber* itNumber;

document.write("The value of ITNumber is: " + itNumber);
// The value of itNumber is: Infinity


If you perform a certain operation involving Number but violate the calculation rules, the result will return a value called NaN (Not a Number). For example, when you divide two numbers, if you give the denominator a String, the result will be NaN (Not a Number). However, if the denominator is a string Number, the result will still work properly. You can be checked by isNaN() method. The return result of isNaN(), if it is NaN, returns true. Otherwise, return false. 

Code sample:

 var a = Number.isNaN(NaN) //true
 var b = Number.isNaN(8888) //false

Number methods in JS

The processing functions themselves are divided into two main groups, the first is the global group and the second is the local group. The global group is the functions that are not in the Number object, and the local group is the functions that are in the Number object.

Global JS Methods

Number(type)returns a number and radix format type = (binary, decimal, hexadecimal)
parseFloat()converts to a float
parseInt()converts to an integer

Local JS Methods

toString()converts to string
toFixed(n)converts to a number with n odd numbers behind it
toPrecision(n)converts to a number of length n
valueOf()get the value of a variable or a certain value


To convert a variable of type Number to String type, we use the number.toString(type) method, this function has a type as an input parameter and this is the data type that you want to convert, by default will be the decimal system (10).




  • number is the number we want to convert.
  • type is one of the numeric types we want to convert to. With the following values:

2 – Binary system.

8 – The octal system.

10 – Decimal system.

16 – Hexadecimal system.

Code sample:

let x = 811;

x.toString(); // 811
(811).toString(); // 811
(800 + 11).toString(); // 811


The toExponential() method is used to return the exponential sequence of a number.




  • number is the number we need to convert.
  • n is the number of elements you want to round after the sign ‘.’

Code sample:

let a = 8.223; 

a.toExponential(2); // 8.22e+0
a.toExponential(4); // 8.2230e+0
a.toExponential(6); // 8.223000e+0


This function has the effect of rounding the number to n elements after the sign ‘.’




  • number is the number we need to convert.
  • n is the number of elements you want to round after the ‘.’

Code sample:

let a = 8.223;

a.toFixed(0); // 8
a.toFixed(2); // 8.22
a.toFixed(4); // 8.2230
a.toFixed(6); // 8.223000


This function converts a number to a number of a specified length.




  • number is the number we need to convert.
  • n is the length of the number you need to convert.

Code sample:

let a = 8.223;

a.toPrecision(); // 8.223
a.toPrecision(2); // 8.2
a.toPrecision(4); // 8.223
a.toPrecision(6); // 8.22300


The valueOf() function has the effect of getting the value of a variable or another value – the meaning is getting the value of (something).

This function is not really used much because usually we take the value directly.



Code sample:

let a = 811;

a.valueOf(); // 811
(811).valueOf(); // 811
(800 + 11).valueOf(); // 811

Thus, we have helped you summarize the most basic knowledge of Javascript Numbers. The system of knowledge will help you have an overview and can easily absorb a large amount of knowledge. Mastering the basics will make the coding process simpler.

Below we have compiled a list of tutorials related to JS numbers that may be of interest to you:

Leave a Reply

Your email address will not be published. Required fields are marked *