--- id: javascript-numbers title: "JavaScript Numbers" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["numbers", "JS numbers", "floating point", "NaN", "Infinity", "IEEE 754"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.89 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "numbers", "floating-point", "nan"] raw_sources: ["https://www.w3schools.com/js/js_numbers.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Numbers]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) JavaScript has only one number type β€” a 64-bit IEEE 754 double β€” so integers are precise only to 15 digits, floating-point arithmetic can be imprecise, and special values `NaN` and `Infinity` are themselves of type `number`. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **One number type** β€” JavaScript has only one type of number; numbers can be written with or without decimals, and scientific notation (`123e5`) is allowed. [S1] - **Always 64-bit floating point** β€” JavaScript numbers are always stored as double precision floating point following the IEEE 754 standard (fraction in bits 0-51, exponent in 52-62, sign in bit 63). [S1] - **Integer precision** β€” integers are accurate up to 15 digits; the maximum number of decimals is 17. [S1] - **Floating precision** β€” floating point arithmetic is not always 100% accurate (e.g. `0.2 + 0.1`). [S1] - **`+` is addition AND concatenation** β€” numbers are added; strings are concatenated; mixing them concatenates left-to-right. [S1] - **`NaN`** β€” a reserved word meaning "not a legal number"; `typeof NaN` returns `number`; use `isNaN()` to test. [S1] - **`Infinity`** β€” returned when a value exceeds the largest possible number, or on division by 0; `typeof Infinity` returns `number`. [S1] - **Hexadecimal** β€” constants prefixed with `0x` are interpreted as hexadecimal; `toString()` can output base 2 to base 36. [S1] - **Numbers as objects** β€” `new Number()` creates an object; the page warns not to create Number objects. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Fix floating drift** β€” multiply and divide (`(0.2 * 10 + 0.1 * 10) / 10`) to work around floating-point imprecision. [S1] - **Guard against NaN** β€” test inputs with `isNaN()` before using them in arithmetic, since `NaN` propagates through operations. [S1] - **Force numeric addition** β€” beware that `"10" + 20` concatenates; ensure operands are numbers when you intend addition. [S1] - **Base conversion** β€” use `number.toString(radix)` to render in binary, octal, hex, or any base 2-36. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Number Types** JavaScript has only one type of number. Numbers can be written with or without decimals. [S1] ```javascript let x = 3.14; // A number with decimals let y = 3; // A number without decimals ``` Extra large or extra small numbers can be written with scientific (exponent) notation: [S1] ```javascript let x = 123e5; // 12300000 let y = 123e-5; // 0.00123 ``` **JavaScript Numbers are Always 64-bit Floating Point** Unlike many other languages, JavaScript does not define different number types (integers, short, long, float, etc.). Numbers are always stored as double precision floating point following IEEE 754, in 64 bits: the fraction in bits 0-51, the exponent in bits 52-62, and the sign in bit 63. [S1] | Value (aka Fraction/Mantissa) | Exponent | Sign | |---|---|---| | 52 bits (0 - 51) | 11 bits (52 - 62) | 1 bit (63) | JavaScript numbers are always double (64-bit floating point). [S1] **Integer Precision** Integers (numbers without a period or exponent notation) are accurate up to 15 digits. The maximum number of decimals is 17. [S1] ```javascript let x = 999999999999999; // x will be 999999999999999 let y = 9999999999999999; // y will be 10000000000000000 ``` **Floating Precision** Floating point arithmetic is not always 100% accurate: [S1] ```javascript let x = 0.2 + 0.1; ``` To solve the problem above, it helps to multiply and divide: [S1] ```javascript let x = (0.2 * 10 + 0.1 * 10) / 10; ``` **Adding Numbers and Strings** > Warning: JavaScript uses the `+` operator for both addition and concatenation. Numbers are added. Strings are concatenated. [S1] Adding two numbers gives a number: [S1] ```javascript let x = 10; let y = 20; let z = x + y; ``` Adding two strings gives a concatenation: [S1] ```javascript let x = "10"; let y = "20"; let z = x + y; ``` Adding a number and a string gives a concatenation: [S1] ```javascript let x = 10; let y = "20"; let z = x + y; ``` Adding a string and a number gives a concatenation: [S1] ```javascript let x = "10"; let y = 20; let z = x + y; ``` A common mistake is to expect this to be 30: [S1] ```javascript let x = 10; let y = 20; let z = "The result is: " + x + y; ``` A common mistake is to expect this to be 102030: [S1] ```javascript let x = 10; let y = 20; let z = "30"; let result = x + y + z; ``` The JavaScript interpreter works from left to right. First `10 + 20` is added because `x` and `y` are both numbers; then `30 + "30"` is concatenated because `z` is a string. [S1] **Numeric Strings** JavaScript will try to convert strings to numbers in numeric operations. Division, multiplication, and subtraction work; addition concatenates. [S1] ```javascript let x = "100"; let y = "10"; let z = x / y; ``` ```javascript let x = "100"; let y = "10"; let z = x * y; ``` ```javascript let x = "100"; let y = "10"; let z = x - y; ``` This will NOT work (it concatenates): [S1] ```javascript let x = "100"; let y = "10"; let z = x + y; ``` **NaN - Not a Number** `NaN` is a JavaScript reserved word indicating that a number is not a legal number. Arithmetic with a non-numeric string yields `NaN`: [S1] ```javascript let x = 100 / "Apple"; ``` If the string is numeric, the result is a number: [S1] ```javascript let x = 100 / "10"; ``` Use `isNaN()` to test: [S1] ```javascript let x = 100 / "Apple"; isNaN(x); ``` `NaN` propagates through math: [S1] ```javascript let x = NaN; let y = 5; let z = x + y; ``` Or it concatenates like `NaN5`: [S1] ```javascript let x = NaN; let y = "5"; let z = x + y; ``` `typeof NaN` returns `number`: [S1] ```javascript typeof NaN; ``` **Infinity** `Infinity` (or `-Infinity`) is returned for a number outside the largest possible number. [S1] ```javascript let myNumber = 2; // Execute until Infinity while (myNumber != Infinity) { myNumber = myNumber * myNumber; } ``` Division by 0 also generates `Infinity`: [S1] ```javascript let x = 2 / 0; let y = -2 / 0; ``` `typeof Infinity` returns `number`: [S1] ```javascript typeof Infinity; ``` **Hexadecimal** JavaScript interprets numeric constants as hexadecimal if they are preceded by `0x`. [S1] ```javascript let x = 0xFF; ``` Never write a number with a leading zero (like `07`); some versions interpret it as octal. By default JavaScript displays numbers as base 10, but `toString()` can output base 2 to base 36 (hex = base 16, decimal = base 10, octal = base 8, binary = base 2): [S1] ```javascript let myNumber = 32; myNumber.toString(32); myNumber.toString(16); myNumber.toString(12); myNumber.toString(10); myNumber.toString(8); myNumber.toString(2); ``` **JavaScript Numbers as Objects** Numbers are normally primitive values from literals, but can be defined as objects with `new`: [S1] ```javascript let x = 123; let y = new Number(123); ``` > Warning: Do not create Number objects. The `new` keyword complicates the code and slows down execution speed. [S1] With `==`, a primitive and a Number object are equal; with `===` they are not equal; and comparing two Number objects always returns false: [S1] ```javascript let x = 500; let y = new Number(500); ``` ```javascript let x = new Number(500); let y = new Number(500); ``` ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own snippets are the canonical applied examples β€” demonstrating integer precision limits, the `0.2 + 0.1` floating drift fix, `+` concatenation pitfalls, `isNaN()` checks, the `Infinity` loop, and `toString(radix)` base conversions. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Work around floating drift: ```javascript let x = (0.2 * 10 + 0.1 * 10) / 10; ``` Detect a non-number: ```javascript isNaN(100 / "Apple"); ``` Convert to another base: ```javascript (32).toString(2); // binary (32).toString(16); // hexadecimal ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) Note that `0.2 + 0.1` does not produce exactly `0.3` due to IEEE 754 floating point; the page presents the multiply/divide workaround. No contradictions found in the source. ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.89 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript Number Methods]], [[JavaScript Data Types]], [[JavaScript Operators]], [[JavaScript BigInt]] - **μ°Έμ‘° λ§₯락:** The foundation for all numeric computation and the precision pitfalls behind money/financial calculations. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Numbers β€” https://www.w3schools.com/js/js_numbers.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Numbers" page (Astra wiki-curation, P-Reinforce v3.1 format).