DEV Community

Cover image for How Does JavaScript Handle Numbers in Memory?
Pooja Pawar
Pooja Pawar

Posted on

How Does JavaScript Handle Numbers in Memory?

Every number in JavaScript allocates 8 bytes of memory. Whether you type 5, -100, or 3.14159 it will allocate 8 byte of memory

Therefore JavaScript stores a number in 64 boxes

These 64 bits are divided into three sections

  1. Sign (s)
  2. Exponent (e)
  3. Mantissa (m)

Let's take an example.
let a = 10;

Step1 : Convert the no into the binary format
10 = 1010

Step2 : Normalized form
1. we can write 1010 as 1010.0 as we write 10 as 10.0 in decimal
2. The rule for the IEEE 754 format is to move the binary point until there is only one '1' to the left of it so it becomes 1.010
3. Since we moved the point 3 places to the left, the original number is equal to 1.010 multiplied by 2^3

This normalized form gives us the three components we need:

  1. Sign: if the number is negative sign= 1 and if the number is positive then the sign= 0
  2. Exponent: The power of 2 is 3.
  3. Mantissa (Significand): The digits after the binary point are 010.

Step3 : Determine the 64-Bit Components

  1. Since 10 is a positive number, the single sign bit is 0.

  2. The exponent field stores the actual power (3 in this case) plus a fixed number called the bias. The bias for the 64-bit format is 1023

    1026 is converted to its 11-bit binary form:

3.The mantissa is 010 Since the mantissa field has 52 bits

how a = 10 is stored in the memory of a JavaScript engine.

In box format:

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.