A Platform To Learn To Code


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

Run the code

Creating Javascript Number Using Constructor

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

Example: Number using Constructor

Run the code

Ways to write a number in Javascript

Generally there are three ways of writing a Number in javascript.

  1. Decimal method
  2. Exponential method
  3. Other Number SystemOther Number System

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.

Javascript also 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.

Example: Exponential method

Run the code

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

Run the code

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

Run the code

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

Run the code

Javascript Number Property

As Number is a global object it has some 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. 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

Example: Number Properties

Run the code

Javascript Number methods

Method Description
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

Run the code