Home Videos Exercises MCQ Q&A Quiz E-Store Services Blog Sign in Appointment Payment

JavaScript Numbers

The Number is a primitive data type used for positive or negative integer, float, binary, octal, hexadecimal, and exponential values in JavaScript.

The Number type in JavaScript is double-precision 64 bit binary format like double in C# and Java. It follows the international IEEE 754 standard.

The first character in a number type must be an integer value, and it must not be enclosed in quotation marks. The following example shows the variables having different types of numbers in JavaScript.

Example:
var n1 = 100; // integer
var n2 = -100; //negative integer

var n3 = 10.52; // float
var n4 = -10.52; //negative float

var n5 = 0xfff; // hexadecimal
var n6 = 256e-5; // exponential
var n7 = 030; // octal
var n8 = 0b0010001; // binary


Integers

Numbers can be positive or negative integers. However, integers are floating-point values in JavaScript. Integers value will be accurate up to 15 digits in JavaScript. Integers with 16 digits onwards will be changed and rounded up or down; therefore, use BigInt for integers larger than 15 digits.

//16 digit integer
var n1 = 1234567890123456; //accurate

//17 digit integer
var n2 = 12345678901234569; //will be 12345678901234568

//16 digit integer
var n3 = 9999999999999998; //will be 9999999999999998

//16 digit integer, last digit 9
var n4 = 9999999999999999; //will be 10000000000000000


Bigint

The BigInt type is a numeric primitive type that can store integers with arbitrary precision. Use the BigInt for the large integers having more than 15 digits. Append n to the end of an integer to make it BigInt.

//16 digit integer
var int1 = 1234567890123459n; //will be 1234567890123459


//17 digit integer
var int2 = 12345678901234569n; //will be 12345678901234569

//20 digit integer
var int3 = 9999999999999999999n; //will be 9999999999999999999


Floating-point Numbers

The floating-point numbers in JavaScript can only keep 17 decimal places of precision; beyond that, the value will be changed.

Example Floating point numbers:
//17 decimal places
var f1 = 123456789012345.9; //accurate

//18 decimal places
var f2 = 1234567890123456.9; //will be 1234567890123457

//19 decimal places
var f3 = 1234567890123456.79; //will be 1234567890123456.8

Arithmetic operations on floating-point numbers in JavaScript are not always accurate. For example:

Example: Arithmetic operations on floating-point numbers
var f1 = 4.1 + 5.2; //will be 9.3

var f2 = 10.1 + 10.2; //will be 20.299999999999997

var f3 = (10.1*100 + 10.2*100)/100; //instead of 10.1 + 10.2


Binary, Octal, Hexadecimal, Exponential

The binary numbers must start with 0b or 0B followed by 0 or 1.

The octal numbers must start with zero and the lower or upper letter 'O', 0o or 0O.

The Hexadecimal numbers must start with zero and the lower or upper letter 'X', 0x or 0X.

The exponential numbers should follow the beN format where b is a base integer or float number followed by e char, and N is an exponential power number.

Example:
var b = 0b100; // binary
var oct = 0o544; // octal
var hex = 0x123456789ABCDEF; // hexadecimal
var exp = 256e-5; // exponential


Number() function in JavaScript

The Number() is a constructor function in JavaScript that converts values of other types to numbers.

Example:
var k = Number('100');
var l = Number('10.5');
var m = Number('0b100');
typeof(k); // returns number
typeof(l); // returns number
typeof(m); // returns number

By using the new operator with the Number() function will return an object which contains constants and methods for working with numbers.

Example Number object:
var i = new Number('10');
var j = new Number('10.5');
var k = new Number('0b100');
typeof(i); // returns object
typeof(j); // returns object
typeof(k); // returns object


Compare Numbers

Be careful while comparing numbers using == or === operators. The == operator compares object references and not the values whereas the === operator compare values. The following example compares numbers created by different ways.

var num1 = new Number(100);
var num2 = Number('100');
var num3 = 100;

num1 == num2; // true
num1 === num2; // false

num2 == num3;//true
num2 === num3; // true

num1 == num3;//true
num1 === num3;//false


Number Properties

The Number type includes some default properties. JavaScript treats primitive values as objects, so all the properties and methods are applicable to both literal numbers and number objects.

The following table lists all the properties of Number type.

Property Description
MAX_VALUE Returns the maximum number value supported in JavaScript
MIN_VALUE Returns the smallest number value supported in JavaScript
NEGATIVE_INFINITY Returns negative infinity (-Infinity)
NaN Represents a value that is not a number.
Positive_Infinity Represents Positive infinity

Number Methods

The following table lists all the methods of Number type

Method Description
toExponential(fractionDigits) Returns exponential value as a string.
toFixed(fractionDigits) Returns string of decimal value of a number based on specified fractionDigits.
toLocaleString() Returns a number as a string value according to a browser's locale settings.
toPrecision(precisionNumber) Returns number as a string with specified total digits.
toString() Returns the string representation of the number value.
valueOf() Returns the value of Number object.