Open In App
Related Articles

How numbers are stored in JavaScript ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In this article, we will try to understand how numbers are stored in JavaScript. Like any other programming language, all the data is stored inside the computer in the form of binary numbers 0 and 1. Since computers can only understand and process data in the form of 0’s and 1’s.

In JavaScript, there is generally only a Number as a primitive data type but the numbers are of the form Integer and float. So these types come under the Number data type and are internally stored according to their different formats depending upon the type of operation being performed on them.

Let us now understand how different numbers are stored in JavaScript.

Storing of Integer Numbers

Integer: These numbers are further divided into two types 

  • Signed Integers
  • Unsigned Integers.

To store unsigned integers simple binary format is used.

Below are a few examples of how different unsigned integers are stored in JavaScript.

let a = 4
let b = 78

a will be stored in the memory with the value of:- 100
b will be stored in the memory with the value of:- 1001110

Note: This method fails when working on unsigned integers as extra data is required to store the sign of numbers.

There is a convention to store the leftmost bit as a sign bit and use 0 for positive and 1 if negative number.

This method is known as Signed Magnitude

The below example illustrates how we will store the number in Signed Magnitude format.

let a = 6
let b = -6

a will be stored in memory with the value of :- 000110
b will be stored in memory with the value of :- 100110

A problem arises when we try to perform addition on these two values. the addition performed on the binary numbers returns

100110 + 000110 = 101100 // 44

We should get 0 when we add the two numbers but instead, we get 44

A further improvement of this method was implemented and one’s complement representation was used to store the number

One’s complement of a number is toggling all the 0’s into 1 and 1’s to 0. Suppose there is a binary number 11001001, then its one’s complement will be 00110110. 

Now let us understand how this method is used to store numbers in the memory.

Let us suppose the number in binary format is 100111, which will be treated as a negative number since 1 is the leftmost bit. Now its one’s complement is 011000 which is 24 in the decimal format so we will treat 100111 as -24. 

In this method, the problem which arose in the previous method is partially solved as addition now starts giving correct results but there is still some exceptions such as when we add 3 and -2 using this representation we get wrong output

3 -> 000011 
-2-> 111101
After addition we get -> 000000 which is +0

Output was expected to be 1.

To further enhance this approach the numbers two complement format was introduced

Two’s complement of a number is similar to One’s complement with one extra step that after finding the one’s complement of the number 1 is added again to the result.

The two complements in the previous of 24(011000) will be 101000. This number when added with the original number 24 we get 000000 as output where the carry bit is ignored and the arithmetic operation passes.

24 -> 011000
two's complement of 24 -> 101000
Addition gives -> 1000000
// One is ignored as digits upto 6 bits are counted

So -24 will be stored as 101000 in the memory

Conclusion: 

When storing Integers in unsigned bit simple binary format is followed 

In order to store signed bit integers, the numbers internally will be stored as two’s complement so as to increase the storage capacity and improve arithmetic calculation.

Storing of Floating point numbers.

To store a float number we divide it into three parts sign bit, exponent, and mantissa.

  • sign bit: It is used to indicate a sign of a number with convention as 0 for positive and 1 for negative.
  • exponent: It is the difference between a real binary number and its normalized form.
  • mantissa: It is used to store the fraction part of the floating point number’s normalized form.

Floating point numbers represented using 32-bit format are called single precision whereas the numbers represented using 64-bit format are called double precision

Storage Space required for storing number in different formats:

 sign exponentmantissa
single1 bit8 bits23 bits
double1 bit11 bits52 bits

The concept of bias is used in the floating point representation to represent negative numbers too instead of one’s and two’s complement. Bias is used because comparison becomes difficult in the case of floating point numbers with two’s complement representations.

For eg., a normal 6-bit representation will follow numbers from 000000 to 111111 but IEEE has given a formula that will store the negative numbers too. If n is the number of bits the formula will be.

bias = 2(n-1) - 1
// Here n = 6
therefore bias = 31

This means in 6-bit representation we can store numbers from -31 to 32 

Let us see how the binary number 101.101 will be represented in scientific notation.

we will get 1.01101 * 2^2 

  • Here, the sign bit is 0  and will be the leftmost bit
  • The exponent is 2. It will indicate the distance between the original binary number and the normalized form.
  • The mantissa is the fraction part which will be 01101

Storing of Irrational Numbers.

Computers are still incapable to store and manipulate irrational numbers so they cannot be stored.

How Numbers are stored internally in JavaScript:

Now let us understand how JavaScript internally manages to store the numbers. JavaScript does not have a separate format for Integer and Float representation. It uses Numbers for all types of numbers. Inside JavaScript, every number is stored as 64 floating-point numbers. In some cases for the arithmetic operation of integers, two’s complement format is used.

Problems with Floating Point Numbers in JavaScript

  • The floating point numbers are not always accurate and give approximate results.

Example: In this example, we will try to understand the above problem.

Javascript

let x = 0.1;
let y = 0.2;
console.log(x+y);

                    

Output: Here, instead of real calculation an approximate result is given for fast performance

0.30000000000000004
  • Floating numbers give different output depending on their associativity

Example: We will understand the associativity problem with the example given below.

Javascript

let x = 0.1;
let y = 0.2;
let z = 0.3
console.log((x+y)+z);
console.log(x+(y+z));

                    

Output: Since floating point calculations are based on rounding, therefore, the result is not always the same even though it should be the same in real-world applications.

0.6000000000000001
0.6


Last Updated : 22 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads