JavaScript Numbers and Number Methods

In JavaScript, numbers are used for all kinds of math operations — whether you're counting items, measuring prices, working with decimals, or even using binary or hexadecimal formats


What Is a Number in JavaScript?

In JavaScript, numbers can be:

  • Whole numbers (like 10)

  • Decimals (like 10.5)

  • Binary numbers (like 0b1010)

  • Hexadecimal (like 0xFF)

  • Octal (like 0o10)

  • Exponential (like 1.2e3)

All numbers in JavaScript use a 64-bit floating-point format, just like in languages such as Java and C#. They follow the IEEE 754 standard.


 Number Basics — Examples

    var num1 = 100;         // integer
    var num2 = -100;        // negative integer
    var num3 = 10.52;       // decimal (float)
    var num4 = -10.52;      // negative float
    var num5 = 0xfff;       // hexadecimal
    var num6 = 256e-5;      // exponential notation
    var num7 = 0o30;        // octal
    var num8 = 0b10001;     // binary

Integers in JavaScript

JavaScript can accurately handle integers up to 15 digits. Beyond that, it may round the number.

    var int1 = 1234567890123456;   // accurate
    var int2 = 12345678901234567;  // might get rounded

To work with very large numbers, use BigInt.


 BigInt — For Very Large Integers

BigInt lets you store numbers bigger than 15 digits. Just add n to the end of the number.

    var big1 = 1234567890123456789n; // accurate even with many digits

 Decimal (Floating Point) Numbers

JavaScript can keep decimals accurately up to 17 digits. After that, rounding may happen.

    var f1 = 123456789012345.9;      // OK
    var f2 = 12345678901234567.9;    // may round

  •  Note: Floating-point math isn’t always 100% accurate.

    var a = 10.1 + 10.2;  // returns 20.299999999999997

To fix that:

    var a = (10.1 * 100 + 10.2 * 100) / 100;  // returns 20.3

Numbers and Strings in Math

If you use math operations (like *, /, %) on strings that contain numbers, JavaScript will convert them to numbers:

    var result = "5" * "4";  // 20

But if you use +, it joins the strings:

    var result = 5 + "4";  // "54" (not 9)

 Binary, Octal, Hex, Exponential — Special Formats

TypeFormatExample
Binary0b0b1010
Octal0o0o10
Hexadecimal0x0xFF
Exponentialbase + e1.5e4 = 15000






    var b = 0b100;           // binary
    var oct = 0o544;         // octal
    var hex = 0xABCDEF;      // hex
    var exp = 256e-5;        // exponential

 Convert Other Types to Numbers

Use Number() to convert strings or other types into numbers:

    var a = Number("100");     // 100
    var b = Number("0b100");   // 4

If you use new Number(), it returns a number object, not a primitive:

    var x = new Number("100");
    typeof x; // "object"

Comparing Numbers

Be careful when comparing numbers, especially objects created with new Number().

    var num1 = new Number(100);
    var num2 = Number('100');
    var num3 = 100;

    num1 == num2;  // true (values are same)
    num1 === num2; // false (one is object)

Number Properties

JavaScript provides built-in properties for number limits:

PropertyDescription
Number.MAX_VALUELargest possible number
Number.MIN_VALUESmallest possible number
Number.POSITIVE_INFINITYInfinity
Number.NEGATIVE_INFINITY-Infinity
Number.NaNNot a Number (e.g., invalid calc)








Useful Number Methods
MethodWhat It Does
.toExponential(n)Shows number in exponential format
.toFixed(n)Rounds to n decimal places (returns string)
.toLocaleString()Formats based on user’s location
.toPrecision(n)Returns number with total n digits
.toString()Converts number to string
.valueOf()Returns the primitive value from a Number object









    var num = 100;

    num.toExponential(2); // "1.00e+2"
    num.toFixed(2);       // "100.00"
    num.toLocaleString(); // "100" (may differ by location)
    num.toPrecision(4);   // "100.0"
    num.toString();       // "100"
    new Number(100).valueOf(); // 100

 Summary

JavaScript treats all numbers (whether integer or decimal) as the same Number type, and supports many formats:

  • Supports: decimal, binary, hex, octal, exponential
  •  Use BigInt for large integers
  •  Math with floating points can be tricky
  •  Use Number() to convert other types
  •  Use number methods to format and display values easily