The Number is a primitive data type. Number represents all numerical values like integer, float, exponential etc.
Generally browsers automatically convert Number literals to instances of Number class.
The normal way of defining a number is Number literal. Lets see an example.
Example: Number using Literal Syntax
To create number using number constructor use
Number() as function/constructor and pass the number as argument. Example
Example: Number using Constructor
1. Decimal Method
We use the Decimal system for our general use, like 9,10,12.3,5.5 etc. Defining numbers in this form is the decimal method.
2. Exponential Method
While writing numbers line 1 billion or 1 trillion in numbers we would have to write like 100000000 and 10000000000 respectively.
"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.
Example: Exponential method
3. Other Number System
There exist other number systems apart from decimal like binary, octal and hexadecimal.
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.
Example: Hexadecimal number
Binary number number includes only 2 numbers 0 and 1. It is defined by
0b in the starting of the number. example 0b1010, 0x11 etc.
Example: Binary number
Octal number includes 8 numbers 0,1,2,3,4,5,6 and 7. It is defined by
0o in the starting of the number. example 0o45, 0x22 etc.
Example: Octal number
As Number is a global object it has some properties.
All the properties of Number are static like properties of math.
|Number.EPSILON||The smallest interval between two numbers|
|Number.MAX_VALUE||The largest positive representable number|
|Number.MIN_VALUE||The smallest positive representable number. Smallest number just greater than 0|
|Number.NaN||Not a Number|
|Number.NEGATIVE_INFINITY||Represent negative Infinity|
|Number.POSITIVE_INFINITY||Represent positive Infinity|
Example: Number Properties
|toString()||Return number as a string|
|toExponential()||Return number as string with number written in exponential form|
|toFixed()||Return number as a string with number of decimals specified|
|toPrecision()||Return number as a string, with specified length|
|valueOf()||Return calculated value in number, we can pass expression a argument|
Example: Number Method