**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
//or
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
```

## NaN

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

Method | Meaning |

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

Method | Meaning |

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 |

### toString()

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

**Syntax:**

`number.toString(type);`

**Parameter:**

**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
```

### toExponential()

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

**Syntax:**

`number.toExponential(n);`

With:

**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
```

### toFixed()

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

**Syntax:**

`number.toFixed(n);`

With:

**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
```

### toPrecision()

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

**Syntax:**

`number.toPrecision(n);`

with:

**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
```

### valueOf()

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.

**Syntax:**

`number.valueOf();`

**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:**

- Get the Decimal Part of a Number in JavaScript
- How to add Leading Zeros to a Number in JavaScript
- How To Get The Sum Of An Array Of Numbers In JavaScript
- Extract A Number From A String In JavaScript
- Check If A Number Is Between Two Numbers In JavaScript
- How To Check If String Is A Valid Number In Javascript
- Check if a Value is an Integer in JavaScript
- Convert a string to an integer in javascript
- Check if a Number is NaN in JavaScript
- Convert a Negative Number to Positive in JavaScript
- Count the Number of Digits in a String in JavaScript
- How to get the Length of a Number in JavaScript
- How to Remove all Characters Except Numbers in JavaScript
- How to Pad a number with Leading Zeros in JavaScript
- Add Strings as Numbers in JavaScript
- Check if a Character is a Number using JavaScript
- Convert Number to Hexadecimal in JavaScript
- Check if String is a Positive Integer using JavaScript
- How To Convert A Positive Number To Negative Using JavaScript?
- Remove The Leading Zeros From A Number Using JavaScript
- Round Down a Number in JavaScript
- Check if a Number is a Negative Integer in JavaScript
- How to Check if a Value is a Number in JavaScript
- Sum all the Digits in a Number in JavaScript
- Get the Last Digit of a Number in JavaScript
- Check if Value is a Negative Number in JavaScript
- Get the second Digit of a Number in JavaScript
- How to Concatenate Two Numbers in JavaScript
- Get the Difference between Two Numbers in JavaScript
- Round a Number Up to the Nearest 10 in JavaScript
- Round a Number to 3 Decimal Places in JavaScript
- Check if one Number is Multiple of another in JavaScript
- Remove Trailing Zeros from a Number using JavaScript
- How to get a Random Float in a Range using JavaScript
- Round a Number to 2 Decimal Places in JavaScript
- Check if Number is not Greater than 0 in JavaScript
- Check if a Value is NOT equal to 0 using JavaScript
- How to Parse Float with 2 Decimal places in JavaScript
- Format a Number as a Percent in JavaScript
- Get the Sum of all Numbers in a Set using JavaScript

Hi guys, wellcome to LearnShareIt. I’m Tiffany and I’m also who responsible for the quality of this website’s content. I previously worked on software development projects for ten years as a developer. Hopefully, our project will bring a lot of value to the community.

**Job: **Programmer/Project management

**Major**: IT

**Programming Languages**: Python, C, HTML, MySQL, SQL Server, C#, C++, Javascript, CSS, Java, VB