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.

JavaScript Editor
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.

JavaScript Editor
String to Number
  • parseInt() - Integer, specify base
  • parseFloat() - Floating point
  • Number() - Strict conversion
  • + - Unary plus operator
Number Checking
  • isNaN() - Global (coerces)
  • Number.isNaN() - Strict
  • isFinite() - Global
  • Number.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.

JavaScript Editor
๐Ÿ“Œ 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.

JavaScript Editor
Common Math Methods:
  • Math.abs() - Absolute value
  • Math.round/floor/ceil/trunc() - Rounding
  • Math.min/max() - Min/Max values
  • Math.random() - Random number 0-1
  • Math.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 2
  • Math.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.

JavaScript Editor
โš ๏ธ 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.

JavaScript Editor
๐Ÿ’ช 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()