Programming Search⌘K

Featureintro

Numbers

Also called Number, number type, number literals, Math, parseInt, parseFloat, toFixed, numeric separators.

JavaScript numbers are IEEE 754 doubles. Literals can be decimal, binary, octal or hex. Number has constants and tests, Math has functions, and Number() and parseInt read strings.

Language
JavaScript

Synopsis

Form Example Value
decimal 42, 3.5 42, 3.5
exponent 175e-2 1.75
binary 0b1010 10
octal 0o644 420
hexadecimal 0xFF 255
with separators 1_000_000 1000000

"You can use four types of number literals: decimal, binary, octal, and hexadecimal."1 An underscore "can be inserted between digits to improve readability."1

console.log(42, 3.5, 175e-2, 0b1010, 0o644, 0xff, 1_000_000);
console.log(typeof 42, typeof 3.5, Number.isInteger(42.0));
Output
42 3.5 1.75 10 420 255 1000000
number number true

Semantics

r1. One number type. "In JavaScript, numbers are implemented in double-precision 64-bit binary format IEEE 754". "Integer values up to ±2^53 − 1 can be represented exactly."1 Integers and fractions share this type, so 42 and 42.0 are the same value. Example: synopsis. See Floating-point numbers.

r2. Three special values. Also, "the number type has three symbolic values: Infinity , -Infinity , and NaN (not-a-number)."1 See NaN and Infinity.

r3. Legacy octal. A literal such as 0644 is a legacy octal. MDN says that "number literals prefixed with 0 , whether interpreted as octal or decimal, cause a syntax error in strict mode".1 Use the 0o prefix. Example: legacy-octal.

r4. The Number object. "The built-in Number object has properties for numerical constants, such as maximum value, not-a-number, and infinity." It also has the tests Number.isFinite(), Number.isInteger(), Number.isNaN() and Number.isSafeInteger().1 toFixed(), toPrecision() and toExponential() return strings.1 Example: number-object.

r5. The Math object. The MDN guide says that "standard mathematical functions are methods of Math ", and that "all trigonometric methods of Math take arguments in radians."1 Example: math.

r6. Number() reads the whole string. Strings "are converted by parsing them as if they contain a number literal . Parsing failure results in NaN ." "Empty or whitespace-only strings are converted to 0 ."2 Example: parse.

r7. parseInt reads a prefix. "The parseInt function converts its first argument to a string , parses that string, then returns an integer or NaN ." It stops at the first character that is not a digit. Without a radix, it reads the 0x prefix as hexadecimal, and "everything else is parsed as a decimal value".3 Example: parse.

Examples

The examples run with Node.js 24.

Constants, tests and formatting (rule r4). The constant values are those in the MDN table:1

console.log(Number.MAX_VALUE, Number.MIN_VALUE, Number.MAX_SAFE_INTEGER);
console.log(Number.isInteger(5.0), Number.isInteger(5.5), Number.isFinite(1 / 0));
const x = 1234.5678;
console.log(x.toFixed(2), x.toPrecision(3), x.toExponential(2), (255).toString(16));
Output
1.7976931348623157e+308 5e-324 9007199254740991
true false false
1234.57 1.23e+3 1.23e+3 ff

Math functions (rule r5):

console.log(Math.round(2.5), Math.round(-2.5), Math.floor(-1.5), Math.trunc(-1.5));
console.log(Math.max(1, 3, 2), Math.max(), Math.sqrt(2), Math.PI);
Output
3 -2 -2 -1
3 -Infinity 1.4142135623730951 3.141592653589793

Number() against parseInt and parseFloat (rules r6 and r7):

console.log(Number("42px"), parseInt("42px"), parseFloat("3.14abc"));
console.log(Number(""), Number(" 12 "), Number("0b11"), parseInt("0b11"));
console.log(parseInt("0x1F"), parseInt("ff", 16), parseInt("08"), parseInt("101", 2));
Output
NaN 42 3.14
0 12 3 0
31 255 8 5

A legacy octal literal is a SyntaxError in strict mode (rule r3):

This code raises SyntaxError, on purpose.

"use strict";
const n = 0755;

Pitfalls

Number('') is 0

An empty input field gives '', and Number('') is 0, not NaN (rule r6).2 Test for the empty string first.

parseInt stops early

parseInt('42px') is 42, and parseInt('0b11') is 0, because it stops at the first character it cannot read (rule r7).3 Use Number() when the whole string must be a number.

toFixed returns a string

(1.5).toFixed(2) is the string '1.50'.1 + on that string joins strings.

Comparison

Topic JavaScript Python
number types one, a binary64 double1 "three distinct numeric types: integers , floating-point numbers , and complex numbers"4
big integers BigInt, a separate type "Integers have unlimited precision."4
fractions the same Number type1 float, "usually implemented using double in C"4

In Python, 42 and 42.0 have different types. In JavaScript they are one value (rule r1). See The float type and BigInts.

References

  1. Numbers and strings, MDN Guide, Numbers; Decimal numbers; Binary numbers; Octal numbers; Hexadecimal numbers; Numeric separators; Number object; Math object
  2. Number, MDN, Number coercion
  3. parseInt(), MDN, Description
  4. Built-in Types, The Python Standard Library, Numeric Types (int, float, complex)

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source