JavaScript Number Methods
Numbers are fundamental in programming. JavaScript provides comprehensive methods for working with numeric data, from basic arithmetic to advanced mathematical operations.
🔢 What are Numbers in JavaScript?
JavaScript has one numeric type: Number. It represents both integers and floating-point numbers using double-precision 64-bit binary format (IEEE 754).
Number Characteristics:
- Double-precision 64-bit floating point
- Integers are accurate up to 15 digits
- Maximum safe integer: 9,007,199,254,740,991
- Special values: Infinity, -Infinity, NaN
- Supports decimal, binary, octal, and hexadecimal
💡 Number Systems
// Decimal (base 10) let decimal = 42; // Binary (base 2) - prefix 0b let binary = 0b101010; // 42 // Octal (base 8) - prefix 0o let octal = 0o52; // 42 // Hexadecimal (base 16) - prefix 0x let hex = 0x2A; // 42 // Scientific notation let sci = 4.2e1; // 42
Special Number Values:
Infinity - Larger than any number
-Infinity - Smaller than any number
NaN - "Not a Number" (invalid operation)
±0 - Positive and negative zero
🔧 Number Creation and Properties
Explore different ways to create numbers and their built-in properties.
Number Systems
0b1010 // Binary 0o777 // Octal 0xFF // Hex 1e6 // Scientific
Special Values
Infinity -Infinity NaN ±0
Number Properties
MAX_SAFE_INTEGER MIN_SAFE_INTEGER MAX_VALUE MIN_VALUE
🔄 Number Conversion Methods
Convert between strings and numbers, and check for valid numbers.
String to Number
parseInt()- Integer, specify baseparseFloat()- Floating pointNumber()- Strict conversion+- Unary plus operator
Number Checking
isNaN()- Global (coerces)Number.isNaN()- StrictisFinite()- GlobalNumber.isFinite()- Strict
Type Checking
Number.isInteger()Number.isSafeInteger()typeof value === 'number'
✨ Number Formatting Methods
Format numbers for display with fixed decimals, precision, or exponential notation.
📌 Formatting Differences:
- toFixed(n) - n digits after decimal, rounds
- toPrecision(n) - n total digits, may use scientific notation
- toExponential(n) - Scientific notation with n fractional digits
- toString(radix) - String in specified base (2-36)
- toLocaleString() - Locale-specific formatting
🧮 Math Object Methods
The Math object provides mathematical constants and functions.
Common Math Methods:
Math.abs()- Absolute valueMath.round/floor/ceil/trunc()- RoundingMath.min/max()- Min/Max valuesMath.random()- Random number 0-1Math.pow()/**- Exponentiation
Math Constants:
Math.PI- π (3.14159...)Math.E- Euler's number (2.718...)Math.SQRT2- √2 (1.414...)Math.LN2- Natural log of 2Math.LOG2E- Log base 2 of E
🚀 BigInt (ES2020)
BigInt is a built-in object that provides a way to represent whole numbers larger than 2⁵³ - 1.
⚠️ BigInt Limitations:
- Cannot mix with regular Numbers in operations
- Cannot use with Math object methods
- No decimal/fractional part
- Division rounds toward zero
- Not serializable to JSON by default
- Slower than regular Numbers for small values
🎮 Number Practice Exercises
Implement the number manipulation functions below.
💪 Additional Challenges:
- Create a function to format numbers with commas (1,000,000)
- Implement a factorial function using BigInt
- Create a function to convert between number bases
- Build a simple calculator with precision handling
- Implement a function to find greatest common divisor (GCD)
📋 Quick Reference
Conversion:
parseInt(str, radix)parseFloat(str)Number(value)+value(unary plus)BigInt(value)
Formatting:
toFixed(digits)toPrecision(precision)toExponential(digits)toString(radix)toLocaleString()
