# JAVASCRIPT NUMBER

The Number is a primitive data type. In javascript, the Number represents all numerical values like integer, float, exponential, etc.

The javascript Number is always stored in double-precision floating-point numbers means it can store both integral and fractional values.

There are two ways to define numbers in javascript:

1. Number literals
2. Number Constructor

Generally, browsers automatically convert Number literals to instances of Number class.

## Creating Javascript Number Using Constructor

To create a number using number constructor use `Number()` as function/constructor and pass the number as an argument. Example `Number(20)`, `Number(35.5)` etc.

## Ways to write a number in Javascript

Generally, there are three ways of writing a number in javascript.

### 1. Decimal Method

The number system that we use in our daily life is the decimal number system. Numbers in decimal number system ranges from 0-9. Defining numbers using a combination of 0-9 is a decimal number.

### 2. Exponential Method

While writing numbers like 1 billion or 1 trillion in numbers we would have to write like 100000000 and 10000000000 respectively, which is very inconvenient.

Javascript provides a method to write such numbers in an easier manner. We can shorten the number by appending `"e"` to the number and then specifying the number of 0's after it. Example, 1000 can be written as 1e3, 0.002 as 2e-3, 50000 as 5e4 etc. This is the exponential method of writing numbers.

### 3. Other Number System

There exist other number systems apart from decimal number system like binary, octal, hexadecimal, etc.

hexadecimal numbers are widely used in the hex color system.

The hexadecimal number is defined by suffix `0x` in the number. example 0xff, 0x25 etc.

Binary numbers includes only 2 differenet numbers, 0's and 1's. It is defined by adding suffix `0b` in the number. example 0b1010, 0x11 etc.

Octal numbers includes 8 different digits in their numbers 0,1,2,3,4,5,6 and 7. It is defined by adding suffix `0o` in the number. example 0o45, 0o22 etc.

## Javascript Number Properties

Number is a global object in javascript, it has many properties.

All the properties of Number are static like properties of math.

Method Description
Number.EPSILON The smallest interval between two numbers
Number.MAX_VALUE The largest positive representable number
Number.MAX_SAFE_INTEGER Maximum safe integer in javascript is 253 - 1
Number.MIN_VALUE The smallest positive representable number. The smallest number just greater than 0
Number.MIN_SAFE_INTEGER Minimum safe integer in javascript is -(253 - 1)
Number.NaN Not a Number
Number.NEGATIVE_INFINITY Represent negative Infinity
Number.POSITIVE_INFINITY Represent positive Infinity

## Javascript Number methods

Number methods in javascript helps us to work with numbers. Primitive value such as 2, 18, etc can't have properties and methods but when we call methods on these values javascript treats it as an object.

### 1. isNaN In Javascript

The `isNaN()` method is a global function which determines whether the given value is `NaN` (Not A Number) or not. If passed value is `NaN` then it returns true else returns false.

You can alternatively also use `Number.isNaN`.

#### Why do we need the isNaN method?

Unlike other values in javascript, we can't compare a NaN value with another NaN. Since both `NaN == NaN` and `NaN === NaN` are false. Hence, we need the `isNaN` method to check whether a number is NaN or not.

### 2. isFinite In Javascript

The `isFinite()` method is a global function which determines whether the given value is a finite number or not. The passed value is also converted to a number if it is possible.

If passed value is either `NaN`, positive infinity or negative infinity then this method returns `false` else return `true`.

### 3. isInteger In Javascript

The `isInteger()` method determines whether the given value is an integer or not.

If passed value is either `NaN`, positive infinity or negative infinity then this method returns `false` and returns `true` for numbers.

### 4. isSafeInteger In Javascript

The `isSafeInteger()` method determines whether the given value is a safe integer or not.

A safe integer is a number that can be exactly represented as an IEEE-754 double-precision number ie. all integer from -(253 - 1) to (253 - 1).

The `isSafeInteger()` method returns `true` if the value is of type Number and is a safe integer.

### 5. parseFloat In Javascript

The `Number.parseFloat()` or `parseFloat()` method parses an argument and returns a floating-point number. If the number can't be parsed then it returns `NaN`.

You can use it to take out numerial values from real life units like "55kg", "120px", "220km", etc.

If the first character can not be converted to a number then it returns NaN.

### 6. parseInt In Javascript

The `Number.parseInt()` or `parseInt()` method is same as `parseFloat` method but it only extracts integer value from the passed value.

`parseInt` also accepts an optional argument which is radix (the base in mathematical number system).

If the first character can not be converted to a number then it returns NaN.

### 7. toExponential Method In Javascript

The `Number.toExponential()` method converts numbers into exponential notation and returns it as a string.

This method accepts an optional argument which an integer which specify the number of digit after decimal value.

### 8. toFixed Method In Javascript

The `Number.toFixed()` method in javascript formats the passed number using fixed-point notation. It is used to format a number with any specific numbers of digits.

`Number.toFixed()` method has a number argument which specifies the number of digits after decimal point

This method round of number as in maths.

### 9. toLocaleString Method In Javascript

The `Number.toLocaleString()` method converts a Date object to a string using locale conventions.

It accepts an argument called locale used to get locale date for the country. Example en-AU (Australian English), en-IN (Indian English), bn-IN (Bangla India), etc.

### 10. toPrecision Method In Javascript

The `Number.toPrecision()` method change number's precise value up to the passed number and return the number as a string. Example: 125000's precision is 3.

This method accepts a number as an argument which defines precision.

### 11. toString Method In Javascript

The `Number.toString()` method returns a string representing the number which invoked it.

This method accepts an optional argument which represents the base of the number system. If you pass 2 then the number will be converted to binary format and returned as a string. Its default value is 10.

### 12. valueOf Method In Javascript

The `Number.valueOf()` method returns a primitive number for the specified object.