JavaScript Numbers

JavaScript Numbers is section 6 of Beginner’s Guide to JavaScript.

Like as in other languages JavaScript doesn’t have different type of numbers; integers, short, long,floats, doubles…etc. It just only have one type of Number and that can be written with, or without, decimals.

var x = 64.00;    // A number with decimals
var y =44;       // A number without decimals

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

var x = 123e5;    // 12300000
var y = 123e-5;   // 0.00123

Normally JavaScript numbers are primitive values created from literals: var x = 123

But numbers can also be defined as objects with the keyword new: var y = new Number(123)

It the same way we use Strings. Don’t create Number objects. They slow down execution speed, and produce nasty side effects:


Integers (numbers without a period or exponent notation) are considered accurate up to 15 digits. So avoid more than that.

var x = 999999999999999;   // x will be 999999999999999
var y = 9999999999999999// y will be 10000000000000000

The maximum number of decimals is 17, but floating point arithmetic is not always 100% accurate. But in the next step you’ll learn how to make it accurate.

var x = 0.2 + 0.1;         // x will be 0.30000000000000004

To solve the problem above, it helps to multiply and divide:

var x = (0.2 * 10 + 0.1 * 10) / 10;       // x will be 0.3

Numbering Systems

Hexadecimal is a numbering system like as the the binary system where you you have 0s and 1s. In Hex it only have 0 to 9 and A-F. Number 10 is represented with Letter A. It is a base 16 numbering system. JavaScript interprets numeric constants as hexadecimal if they are preceded by 0x.

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

Most importantly Never write a number with a leading zero (like 09). Some JavaScript versions interpret numbers as octal numbering system, if they are written with a leading zero. By default, Javascript displays numbers as base 10 decimals.But you can use the toString() method to output numbers as base 16 (hex), base 8 (octal), or base 2 (binary). Try to put other than following numbers and find what it gives.

var myNumber = 128;
myNumber.toString(16);     // returns 80
myNumber.toString(8);      // returns 200
myNumber.toString(2);      // returns 10000000


Infinity (or -Infinity) is the value JavaScript will return if you calculate a number outside the largest possible number. Infinity is a number: typeOf Infinity returns number.

[code languague=”js”] var myNumber = 2;
while (myNumber != Infinity) { // Execute until Infinity
myNumber = myNumber * myNumber;

Division by 0 (zero) also generates Infinity:

var x =  2 / 0;          // x will be Infinity
var y = -2 / 0;          // y will be -Infinity

NaN – Not a Number

NaN is a JavaScript reserved word indicating that a value is not a number. Trying to do arithmetic with a non-numeric string will result in NaN. However, if the string contains a numeric value , the result will be a number.You can use the global JavaScript function isNaN() to find out if a value is a number. Watch out for NaN. If you use NaN in a mathematical operation, the result will also be NaN. NaN is a number: typeOf NaN returns number.

var x = 100 / “Apple”// x will be NaN (Not a Number)

isNaN(x);               // returns true because x is Not a Number

var x = 100 / “10”;     // x will be 10

Number Properties

Number properties belongs to the JavaScript’s number object wrapper called Number. These properties can only be accessed as Number.MAX_VALUE.

Property Description
MAX_VALUE Returns the largest number possible in JavaScript
MIN_VALUE Returns the smallest number possible in JavaScript
NEGATIVE_INFINITY Represents negative infinity (returned on overflow)
NaN Represents a “Not-a-Number” value
POSITIVE_INFINITY Represents infinity (returned on overflow)

Number Methods

Number methods help you to work with numbers. These are the most relevant global methods, when working with numbers. These 3 JavaScript functions can be used to convert variables to numbers. So they are called global methods.

Method Description
Number() Returns a number, converted from its argument.
parseFloat() Parses its argument and returns a floating point number
parseInt() Parses its argument and returns an integer

Following provide common number functions. They return a new variable. They do not change the original variable.

Method Description
toString() Returns a number as a string
toExponential() Returns a string, with a number rounded and written using exponential notation.
toFixed() Returns a string, with a number rounded and written with a specified number of decimals.
toPrecision() Returns a string, with a number written with a specified length
valueOf() Returns a number as a number



What’s Next?

Learn what includes in JavaScript Dates

Written by Sandeep Likhar

Sandeep Likhar is from India, where he is a blogger, eBook designer, and founder of LetsDnD. He has 6 years of experience in the industry as a Digital Publishing Expert and eBook Converter, providing services to authors, publishers, and distributors worldwide. He is proficient in converting books into various formats, such as HTML, epub, mobi, word, PDF, including all major online platforms like iTunes, Kobo, Kindle, CreateSpace, B&N, Smashwords, and more.

Comment Below