How to deal with numbers in JavaScript

We can deal with numbers in JavaScript in many ways. Lets look at the basics.

The size of a JavaScript number

In JavaScript a number is always a 64 bit floating point. There is no integer, nor decimal, nor bit. Only a 64 bit floating point. This precision follows the international IEEE 754 Standard.

We can divided the the 64 bits as follows:

  • Bits 0 to 51, are the value of the number (also known as fraction or mantissa)
  • Bits 52 to 62, are the exponent of the number
  • Bits 63, is the sign bit

This structure of the number gives us the following:

  • A number, without decimal point or exponent, can only be exact up to 15 digits long
  • A number can have up to 17 decimals numbers

How to create a number in JavaScript

You can create numbers in JavaScript as integers (without any decimal point), or as decimal (with decimal points), or as exponents or scientific notation or as hexadecimal number using the following simple way:

var myIntegerNumber = 234; // integer
var myDecimalNumber = 45.67; // decimal
var myExponentNumber = 345e5; // exponent
var myHexadecimalNumber = 0xFF; // hexadecimal

When creating a number other than a hexadecimal, do not start it with a zero digit, since some browsers or frameworks do not work correctly with it.

You can, also, create a number using the Number object as follows:

var myObjectNumber = new Number(456); // a number object

Be aware than using the number object will affect your performance and will create some problems when comparing numbers in your code, see below for more details.

Converting numbers to strings

We can convert Numbers to a string in two ways: by concatenation or by using the toString() method.

Concatenating is the first way to convert a number to a string. For example:

var newString = "Carlos " + 5; //return: Carlos 5
var newString2 = 5 + "6"; // return: 56, not 11

The second method is to use the toString() method. You will be able to convert a number to a base 10 string, and also to hexadecimal, octal and binary strings.

var myNumber = 128;
var myBase10String = myNumber.toString(); // return: 128
var myHexadecimalString = myNumber.toString(16); // return: 80
var myOctalString = myNumber.toString(8); // return: 200
var myBinaryString = myNumber.toString(2); // return: 10000000

Comparing numbers

We can compare any number using the == or the === equality operators. But we could face some problems, if we are comparing numbers created using the simple way versus a number created using the Number object. When using the == equality operator we will not have any problem.

var s = 123;
var o = new Number(123);
// s == o, will return true since the == equality operator will compare only the value of both numbers

If we use the === equality operator to compare numbers, we will get a different result, since the operator not only compare the value, but also the object.

var s = 123;
var o = new Number(123);
// s === o, will return false, since s and o are different objects

Be aware that creating the number with the same value using the Number object and using the === equality operator also will not return true. Every time we create a new number object, it is a different number object:

var s = new Number(123);
var o = new Number(123);
// s === o, will return false, since s and o are different objects.  

Numbers are easy in JavaScript. They are not as complicated as other languages. Knowing the basics of them will help us to build robust and performance wise applications.

Rodnney

I am a "multi hat" software developer with more than 18 years in experience. I had worked from government agencies, insurance companies, ticketing system and educational business using a lot of technologies like SQL Server, Oracle, ASP.NET, MVC, HTML, CSS, Javascript, Linq, etc.

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.