JavaScript Number

About JavaScript Number

In JavaScript, the 'Number' data type represents both integer (e.g., 25) and floating-point (e.g., 2.231) values.

The JavaScript 'Number' type is a double-precision 64-bit binary format IEEE 754 value, like double in Java or C#.

'Number' can handle up to 15 digits of precision for integers and 17 digits for floating-point numbers.

Number as Integer and Floating-point

In JavaScript, a number can represent both integer as well as floating-point value as demonstrated in the following example:

Output of the above example
The sum of x and y is 12.56
                                                        
Explanation of the code
  • let x = 10;: This line declares a variable named 'x' and assigns integer number value 10.
  • let y = 2.56;: This line declares a variable named 'y' and assigns floating-point number value 2.56.
  • let sum = x + y;: This line add the values of 'x' and 'y' and assigns the sum to the variable 'sum'.
  • console.log("The sum of x and y is " + sum);: In the line, 'console.log()' method prints the 'sum' value in the web console.

Arithmetic Operations with JS Number

Arithmetic operations like addition, subtraction, multiplication, division, and more can be performed with number as demonstrated in the following example:

Output of the above example
The sum of a and b is 15
The difference of a and b is 5
The product of a and b is 50
The quotient of a and b is 2
                                                        

JS Number Constructor

The JavaScript 'Number' constructor can be used as both a function and a constructor to work with numbers as demonstrated in the following examples:

When 'Number' is used with the new keyword, it creates a Number object:

'Number' can be used as a function without the new keyword. It can convert a numeric string value to a number.

If you try to convert a non-numeric string to a number, the output will be NaN (Not a number).

Methods of JavaScript Number

  • Number.parseInt() : This method parses a numeric string and returns an integer (whole number).


  • Number.parseFloat() : This method parses a numeric string and returns a floating-point number.


  • Number.toFixed() : This method converts a number to a string with a fixed number of decimal places.


  • Number.toPrecision() : This method formats a number to a specific length (number of digits).


  • Number.toString() : This method converts a number to a string with the option of converting to a specific base number (e.g., binary, octal, hexadecimal).


  • Number.valueOf() : This method returns the primitive value of a Number object.


  • Number.isNaN() : This method determines whether the value is NaN (Not-a-Number). If the value is 'NaN', it returns true otherwise it returns false.


  • Number.isFinite() : This method determines whether the value is a finite number.


  • Number.isInteger() : This method determines whether the number is an integer.


  • Number.isSafeInteger() : This method determines whether the number is a safe integer (the integer is within the safe range for JavaScript integers).


Properties of JavaScript Number

  • Number.MAX_VALUE : This represents the largest possible numeric value.

  • Number.MIN_VALUE : This represents the smallest possible numeric value.

  • Number.POSITIVE_INFINITY : This represents positive infinity.

  • Number.NEGATIVE_INFINITY : This represents negative infinity.

  • Number.NaN : This represents special NaN "Not-a-Number" value.

  • Number.EPSILON : This represents the smallest difference between two representable numbers (the value of the difference between 1 and the smallest floating-point number greater than 1).

  • Number.MAX_SAFE_INTEGER : This represents the maximum safe integer in JavaScript.

  • Number.MIN_SAFE_INTEGER : This represents the minimum safe integer in JavaScript.

JavaScript Compiler For Live Practice

Practice the above JavaScript code in the following JavaScript Editor. Create more JavaScript numbers, convert between different number types, and perform different arithmetic operations in the following editor and click Execute button to check the output. The output will be displayed in JavaScript Code Output section.

JavaScript logo JavaScript Editor
JavaScript Code Output