# JavaScript Number

In this tutorial, you will learn about JavaScript Number with the assistance of examples.

## Definition and Usage

The Number() function changes the object contention over to a number that represents the object’s value. If the worth can’t be changed over to a legal number, NaN is returned.

The Number JavaScript object is a wrapper object allowing you to work with numerical values. A Number object is made using the Number() constructor. A primitive type object number is made using the Number() function.

In JavaScript, numbers are primitive data types. For instance,

```const a = 3;
const b = 3.13;```

Dissimilar to in some other programming languages, you don’t need to explicitly proclaim for integer or floating values using int, float, and so forth

You can use remarkable notation e to incorporate too huge or too little numbers. For instance,

```const a1 = 5e9;
console.log(a1); //5000000000

const a2 = 5e-5;
console.log(a2); // 0.00005```

Numbers can also be denoted in hexadecimal notation. For example,

```const a = 0xff;
console.log(a); // 255

const b = 0x00 ;
console.log(b); // 0```

## +Operator with Numbers

When + is used with numbers, it is used to add the numbers. For instance,

```const a = 4 + 9;
console.log(a); // 13```

When + is used with numbers and strings, it is used to link them. For instance,

```const a = '4' + 9;
console.log(a); // 49```

At the point when a numeric string is used with other numeric operations, the numeric string is changed over to a number. For instance,

```const a = '4' - 2;
console.log(a); // 2

const a = '4' / 2;
console.log(a); // 2

const a = '4' * 2;
console.log(a); // 8```

## JavaScript NaN

In JavaScript, NaN(Not a Number) is a catchphrase that shows that the worth is definitely not a number.

Performing arithmetic operations (except + ) to numeric incentive with string results in NaN. For instance,

```const a = 4 - 'hello';
console.log(a); // NaN```

The built-in function isNaN() can be used to find if a value is a number. For example,

```const a = isNaN(9);
console.log(a); // false

const a = isNaN(4 - 'hello');
console.log(a); // true```

When the typeof operator is used for NaN value, it gives a number output. For example,

```const a = 4 - 'hello';
console.log(a); // NaN
console.log(typeof a); // "number"```

## JavaScript Infinity

In JavaScript, when the calculation is done that exceeds the biggest (or littlest) conceivable number, Infinity (or – Infinity) is returned. For instance,

```const a = 2 / 0;
console.log(a); // Infinity

const a = -2 / 0;
console.log(a); // -Infinity
```

## JavaScript BigInt

In JavaScript, Number sort can just represent numbers less than (253 – 1)) and more than – (253 – 1). Nonetheless, on the off chance that you need to use a bigger number than that, you can use the BigInt data type.

A BigInt number is made by annexing n to the end of an integer. For instance,

```// BigInt value
const value = 900719925124740998n;

// Adding two big integers
const value1 = value + 1n;
console.log(value1); // returns "900719925124740999n"```

Note: BigInt was introduced in the newer version of JavaScript and is not supported by many browsers. Visit JavaScript BigInt support to learn more.

## JavaScript Numbers Are Stored in 64-bit

In JavaScript, numbers are stored in 64-bit format IEEE-754, also known as “double-precision floating-point numbers”.

The numbers are stored in 64 bits (the number is stored in 0 to 51-bit positions, the example in 52 to 62-bit positions, and the sign-in 63-bit position).

## Precision Problems

Operations on floating-point numbers results in some unexpected results. For example,

```const a = 0.1 + 0.2;
console.log(a); // 0.30000000000000004```

The result ought to be 0.3 rather than 0.30000000000000004. This mistake happens in light of the fact that in JavaScript, numbers are stored in binary form to represent decimal digits inside. Also, decimal numbers can’t be represented in a binary form precisely.

To solve the above issue, you can accomplish something like this:

```const a = (0.1 * 10 + 0.2 * 10) / 10;
console.log(a); // 0.3```

You can also use the toFixed() method.

```const a = 0.1 + 0.2;
console.log(a.toFixed(2)); // 0.30```

toFixed(2) rounds up the decimal number to two decimal values.

```const a = 9999999999999999
console.log(a); // 10000000000000000```

Note: Integers are accurate up to 15 digits.

## Number Objects

You can also create numbers using the new keyword. For example,

```const a = 45;

// creating a number object
const b = new Number(45);

console.log(a); // 45
console.log(b); // 45

console.log(typeof a); // "number"
console.log(typeof b); // "object"```

Note: It is prescribed to try not to use number objects. Using number objects slows down the program.

## JavaScript Number Methods

Here is a list of built-in number methods in JavaScript.

For example,

```// check if a is integer
const a = 12;
console.log(Number.isInteger(a)); // true

// check if b is NaN
const b = NaN;
console.log(Number.isNaN(b)); // true

// display upto two decimal point
const d = 5.1234;
console.log(d.toFixed(2)); // 5.12```

## JavaScript Number Properties

Here is a list of Number properties in JavaScript.

For example,

```// largest possible value
const a = Number.MAX_VALUE;
console.log(a); // 1.7976931348623157e+308

// maximum safe integer
const a = Number.MAX_SAFE_INTEGER;
console.log(a); // 9007199254740991```

## JavaScript Number() Function

The Number() function is used to convert various data types to numbers. For example,

```const a = '23'; // string
const b = true; // boolean

//converting to number
const result1 = Number(a);
const result2 = Number(b);

console.log(result1); // 23
console.log(result2); // 1```

If you want to learn more about the number conversion, visit JavaScript Type Conversion.

Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.