Javascript Numbers

The JavaScript number object allow you to represent a numeric value. It can be integer or floating value. Numbers can be written with or without decimals.

var x = 6.15;    // A number with decimals

var y = 6;       // A number without decimals

  • With the help of Number() constructor, you can create number object in JavaScript. For example:

var num = new Number(value);  

  • Extra large or extra small numbers can be written with scientific (exponent) notation:

var x = 132e5;    // 12300000

var y = 132e-5;   // 0.00123


JavaScript Numbers are Always 64-bit Floating Point

  • JavaScript number object follows IEEE 754 standard to represent the floating point numbers.

This format stores numbers in 64 bits, where the number (the fraction) is stored in bits 0 to 51, the exponent in bits 52 to 62, and the sign in bit 63:

Value (aka Fraction/Mantissa)ExponentSign
52 bits (0 – 51)11 bits (52 – 62)1 bit (63)

If value is not a number and can’t be converted to a number then it returns NaN(Not a Number) that can be validated by isNaN() method.


Precision

Integers are accurate up to 15 digits.

var x = 999999999999999;   // x will be 999999999999999

var y = 9999999999999999;  // y will be 10000000000000000

The maximum number of decimals in floating number is 17, but floating point arithmetic is not always 100% accurate:


JavaScript Number Constants

Let’s have the list of number constants with explanation-

ConstantDescription
MIN_VALUEreturns the largest minimum value.
MAX_VALUEreturns the largest maximum value.
POSITIVE_INFINITYreturns positive infinity, overflow value.
NEGATIVE_INFINITYreturns negative infinity, overflow value.
NaNrepresents “Not a Number” value.

 


Adding Numbers and Strings

var x = 20;
var y = 30;
var z = x + y;           // z will be 50 (a number)

If you add two strings, the result will be a string concatenation:

var x = "20";
var y = "30";
var z = x + y;           // z will be 2030 (a string)

Note:- JavaScript uses the + operator for both addition and concatenation.Numbers will be added and Strings will be concatenated.


Numeric Strings

JavaScript strings may have numeric content:

var x = 200;         // x is a number

var y = “200”;       // y is a string

  • JavaScript will try to convert strings to numbers for all numeric operations:

This will work:

var x = "200";
var y = "10";
var z = x / y;       // z will be 20

NaN – Not a Number

NaN is indicating that a number is not a legal number.

Trying to do arithmetic operation with a non-numeric string will result in NaN (Not a Number):

var x = 100 / "Apple";  // x will be NaN (Not a Number)

Infinity

Infinity (or -Infinity) value will be returned if you calculate a number outside the largest possible number.

var myNum = 3;
while (myNum != Infinity) {   // Execute until Infinity
  myNum = myNum * myNum;
}

typeof Infinity returns number.

typeof Infinity;     // returns "number"

Hexadecimal

JavaScript interprets numeric constants as hexadecimal if they are preceded by 0x.

var x = 0xFF;        // x will be 255

By default, JavaScript displays numbers as base 10 decimals.

But you can use the toString() method to output numbers from base 2 to base 36.

Hexadecimal is base 16. Decimal is base 10. Octal is base 8. Binary is base 2.

var myNum = 32;
myNum.toString(10);  // returns 32
myNum.toString(32);  // returns 10
myNum.toString(16);  // returns 20
myNum.toString(8);   // returns 40
myNum.toString(2);   // returns 100000

Numbers Can be Objects

Normally JavaScript numbers are primitive values created from literals:

var x = 600;

But numbers can also be defined as objects with the keyword new:

var y = new Number(600);

When using the == operator, equal numbers are equal:

var x = 600;             
var y = new Number(600);
// (x == y) is true because x and y have equal values

When using the === operator, equal numbers are not equal, because the === operator expects equality in both type and value.

var x = 600;             
var y = new Number(600);
// (x === y) is false because x and y have different types

Even objects cannot be compared:

var x = new Number(600);             
var y = new Number(600);
// (x == y) is false because objects cannot be compared

Note:- Do not create Number objects. It slows down execution speed. The new keyword complicates the code. This can produce some unexpected results: