Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
In JavaScript, the Number type is a numeric data type representing both integer and floating-point values. Under the hood, all Number values are implemented as double-precision 64-bit floating-point formats conforming to the IEEE 754 standard. While modern JavaScript includes the BigInt type specifically for arbitrary-precision integers, the Number type itself does not distinguish between integers and floats. Consequently, all operations on Number values are subject to floating-point arithmetic rules and limitations.
Memory Architecture (IEEE 754)
A JavaScript Number consumes 64 bits of memory, divided into three components:
- Sign: 1 bit (determines positive or negative).
- Exponent: 11 bits (determines the magnitude).
- Mantissa (Fraction/Significand): 52 bits (determines the precision).
Although only 52 bits are allocated in memory for the mantissa, normalized IEEE 754 numbers use an implicit leading bit of 1 that is not stored. This hidden bit effectively provides 53 bits of precision, allowing the Number type to safely represent integers between -(2^53 - 1) and 2^53 - 1. Values outside this range are subject to precision loss.
Instantiation and Syntax
Numbers can be created via literals, the Number function (for type coercion), or the Number constructor (which creates an object wrapper).
// 1. Numeric Literals (Primitive)
const int = 42;
const float = 3.14;
const hex = 0xff; // Base 16
const binary = 0b1010; // Base 2
const octal = 0o744; // Base 8
const scientific = 1.5e3 // 1500
// 2. Number Function (Type Coercion to Primitive)
const coerced = Number("42"); // 42
const coercedBool = Number(true); // 1
// 3. Number Constructor (Object Wrapper - Generally Avoided)
const numObj = new Number(42);
typeof int; // "number"
typeof numObj; // "object"
Special Numeric Values
The Number type includes several special values defined by the IEEE 754 standard:
const notANumber = NaN; // Result of an invalid mathematical operation
const posInf = Infinity; // Result of dividing a positive number by 0
const negInf = -Infinity; // Result of dividing a negative number by 0
const posZero = +0; // Standard zero
const negZero = -0; // Negative zero (behaves identically to +0 in most cases)
Static Properties
The Number constructor holds several static properties representing architectural limits and constants.
Number.MAX_SAFE_INTEGER; // 9007199254740991 (2^53 - 1)
Number.MIN_SAFE_INTEGER; // -9007199254740991 (-(2^53 - 1))
Number.MAX_VALUE; // 1.7976931348623157e+308 (Largest representable number)
Number.MIN_VALUE; // 5e-324 (Smallest positive representable number)
Number.EPSILON; // 2.220446049250313e-16 (Difference between 1 and the smallest floating point number greater than 1)
Number.NaN; // Special "Not a Number" value
Number.POSITIVE_INFINITY; // Infinity
Number.NEGATIVE_INFINITY; // -Infinity
Static Methods
Static methods are called directly on the Number object and are primarily used for type checking and parsing.
// Parsing
Number.parseInt("42.9px", 10); // 42 (Parses string to integer, accepts radix)
Number.parseFloat("42.9px"); // 42.9 (Parses string to float)
// Type and Value Checking
Number.isFinite("42"); // false (Does not coerce types, unlike global isFinite("42") which returns true)
Number.isFinite(Infinity); // false (Infinity is not a finite number)
Number.isInteger(42.0); // true
Number.isInteger(42.1); // false
Number.isNaN("NaN"); // false (Does not coerce types, unlike global isNaN("NaN") which returns true)
Number.isNaN(NaN); // true
Number.isSafeInteger(9007199254740992); // false (Exceeds 2^53 - 1)
Instance Methods
Instance methods are inherited from Number.prototype and are used to format or convert numeric primitives. When called on a primitive, JavaScript temporarily wraps the primitive in a Number object to execute the method.
const num = 123.456;
// Returns a string representing the number in exponential notation
num.toExponential(2); // "1.23e+2"
// Returns a string representing the number with fixed-point notation
num.toFixed(2); // "123.46" (Rounds the fractional part)
// Returns a string representing the number to a specified precision (total digits)
num.toPrecision(4); // "123.5"
// Returns a string representation of the number in the specified radix (base)
num.toString(16); // "7b.74bc6a7ef9db2" (Hexadecimal representation)
// Returns the primitive value of the Number object
num.valueOf(); // 123.456
Master JavaScript with Deep Grasping Methodology!Learn More