Files
2nd/10_Wiki/Topic_JavaScript/JavaScript_BigInt.md
T
koriweb 9609c04755 docs(10_Wiki): W3Schools 위키화 — HTML/CSS/JavaScript(core)
W3Schools 튜토리얼을 P-Reinforce v3.1 포맷으로 위키화(영어 본문, 한/영 섹션 헤더).
- Topic_HTML: 59문서 (튜토리얼+예제, 레퍼런스/메타 제외)
- Topic_CSS: 190문서 (메인 + Advanced/Flexbox/Grid/RWD 전체)
- Topic_JavaScript: 120문서 (코어 언어; Temporal/DOM상세/BOM/WebAPI/AJAX/jQuery/Graphics 등은 후속)
각 폴더 00_INDEX.md(MOC) 포함. 코드 verbatim, 미확인분은 "Not found in source" 표기.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 19:21:18 +09:00

213 lines
8.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
id: javascript-bigint
title: "JavaScript BigInt"
category: "Frontend"
status: "draft"
verification_status: "conceptual"
canonical_id: ""
aliases: ["BigInt", "arbitrary precision integer", "n suffix", "BigInt()", "ES2020 integers", "large integers"]
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", "bigint", "number", "es2020"]
raw_sources: ["https://www.w3schools.com/js/js_bigint.asp"]
applied_in: []
github_commit: ""
---
# [[JavaScript BigInt]]
## 🎯 한 줄 통찰 (One-line insight)
`BigInt` is a JavaScript data type for whole numbers too large for the ordinary `Number` type — it represents integers of arbitrary size (limited only by memory), but cannot hold decimals and cannot be freely mixed with `Number` values. [S1]
## 🧠 핵심 개념 (Core concepts)
- **Purpose** — `BigInt` handles integers larger than the safe-integer limits of the `Number` type, representing integers of any size constrained only by available memory. [S1]
- **Why it exists** — JavaScript Numbers are 64-bit floating point (IEEE 754) and only accurate up to 15 digits; safe integers run from `9007199254740991` (`2⁵³ 1`) down to `-9007199254740991`. [S1]
- **Two creation forms** — an integer literal with an `n` suffix, or the `BigInt()` constructor (from a string or a Number). [S1]
- **It is a distinct type** — `typeof` a BigInt is `"bigint"`; it is the third numeric-family data type, making eight JavaScript types in total. [S1]
- **No mixing without conversion** — arithmetic between a BigInt and a Number throws a `TypeError`; convert explicitly with `Number()` or `BigInt()`. [S1]
- **No decimals** — a BigInt cannot have decimal values. [S1]
- **ES2020 feature** — supported in modern browsers since September 2020. [S1]
## 🧩 추출된 패턴 (Extracted patterns)
- **`n` literal vs constructor** — `999999999999999n` (literal) or `BigInt("999999999999999")` (string constructor avoids the 15-digit accuracy loss). [S1]
- **Explicit cross-type conversion** — to combine with a Number, wrap one side: `Number(x) + y`. [S1]
- **Loose vs strict equality** — `10n == 10` is `true`, but `10n === 10` is `false` because the types differ. [S1]
- **No `>>>` on BigInt** — unsigned right shift is not allowed with BigInts. [S1]
## 📖 세부 내용 (Details)
**What is JavaScript BigInt?** — `BigInt` is a data type that can represent integers of any size, limited only by available memory. [S1]
**JavaScript Accuracy / Numbers are 64-bits Floating Point** — JavaScript Numbers are only accurate up to 15 digits and use 64-bit floating-point format (IEEE 754). The safe integer range is `9007199254740991` (`2⁵³ 1`) to `-9007199254740991` (`(2⁵³ 1)`). [S1]
```javascript
// 15 digits:
let x = 999999999999999;
// 16 digits:
let y = 9999999999999999;
```
```javascript
// MAX = 9007199254740991
let x = Number.MAX_SAFE_INTEGER;
// MIN = -9007199254740991
let y = Number.MIN_SAFE_INTEGER;
```
```javascript
// Max (accurate)
let x = 9007199254740991;
// Max + 10 (inaccurate)
let y = x + 10;
```
**How to Create a BigInt** — use an integer literal with an `n` suffix, or the `BigInt()` constructor: [S1]
```javascript
// Using an integer literal with an n suffix:
let x = 999999999999999n;
// Using the BigInt() constructor with a string:
let y = BigInt("999999999999999");
```
**BigInt is a JavaScript Datatype**`typeof` returns `"bigint"`: [S1]
```javascript
let x = BigInt(999999999999999);
let type = typeof x;
```
BigInt is the third numeric data type. The eight JavaScript data types are: String, Number, Bigint, Boolean, Undefined, Null, Symbol, Object. [S1]
**Arithmetic Operators** — BigInt supports `+`, `-`, `++`, `--`, `*`, `/`, `%`, `**`: [S1]
```javascript
let x = 9007199254740995n;
let y = 9007199254740995n;
let z = x * y;
```
**Mixing BigInt and Numbers** — mixing throws a `TypeError`; convert explicitly: [S1]
```javascript
let x = 10n;
let y = 5;
let z = x + y; // ❌ TypeError
```
```javascript
let x = 10n;
let y = 5;
let z = Number(x) + y;
```
**BigInt / Number Conversions** — BigInt to Number with `Number(value)`, Number to BigInt with `BigInt(value)`; large conversions may result in Infinity or precision loss. [S1]
**BigInt Decimals** — a BigInt cannot have decimals, and mixing with division throws an error: [S1]
```javascript
let x = 5n;
let y = x / 2;
// ❌ Error: Cannot mix BigInt and other types, use explicit conversion.
```
**Comparison Operators** — BigInt supports `<`, `>`, `==`, `===`, `!==`, `<=`, `>=`. Strict equality between a BigInt and a Number is always false: [S1]
```javascript
// true
let x = (10n > 5n);
// false (different types)
let y = (10n === 10);
// true (loose equality)
let z = (10n == 10);
```
**Bitwise Operators** — BigInt supports `&`, `|`, `^`, `~`: [S1]
```javascript
let a = 5n; // 0101
let b = 3n; // 0011
let x = (a & b); // 1n (0001)
let y = (a | b); // 7n (0111)
let z = (a ^ b); // 6n (0110)
let n = (~a); // -6n
```
**Bitwise Shift Operators** — BigInt supports `<<` and `>>`. Unsigned right shift (`>>>`) is not allowed with BigInts: [S1]
```javascript
let big = 10n; // binary: 1010
let x = (big << 2n); // 40n (101000)
let y = (big >> 1n); // 5n (0101)
```
**BigInt Hex, Octal and Binary** [S1]
```javascript
let num = 256n;
let oct = 0o400n;
let hex = 0x100n;
let bin = 0b100000000n;
```
**Precision Curiosity** — Number rounding makes two distinct large integers compare equal, while BigInt keeps them distinct: [S1]
```javascript
9007199254740992 === 9007199254740993; // is true !!!
```
```javascript
9007199254740992n === 9007199254740993n; // is false !!!
```
**Summary** — BigInt enables arbitrary-precision integer arithmetic. Limitations: no decimal support, incompatibility with `Math` functions, and `JSON.stringify()` cannot handle BigInts. [S1]
**Browser Support** — BigInt is an ES2020 feature, fully supported in modern browsers since September 2020. [S1]
## 🛠️ 적용 사례 (Applied in summary)
The page's own snippets are the canonical applied examples — creating BigInts via the `n` literal and the `BigInt()` constructor, multiplying large values, the explicit `Number(x) + y` conversion, and the precision-comparison curiosity. No external project/commit applications found in the source.
## 💻 코드 패턴 (Code patterns)
Create a BigInt (language: JavaScript):
```javascript
let a = 999999999999999n;
let b = BigInt("999999999999999");
```
Mix safely with a Number via explicit conversion:
```javascript
let x = 10n;
let y = 5;
let z = Number(x) + y;
```
Compare with loose vs strict equality:
```javascript
10n == 10; // true
10n === 10; // false
```
## ⚖️ 비교 및 선택 기준 (Comparison & decision criteria)
- **Use `Number`** for everyday math and any value within the safe-integer range (`±9007199254740991`) or requiring decimals. [S1]
- **Use `BigInt`** when integers exceed the safe-integer range and must stay exact (the page cites cryptography, IDs, and timestamps), accepting that decimals, `Math` functions, and `JSON.stringify()` are unavailable. [S1]
- **Never mix the two directly** — arithmetic across the types throws a `TypeError`; convert one side explicitly. [S1]
## ⚖️ 모순 및 업데이트 (Contradictions & updates)
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 Numbers]], [[JavaScript Number Properties]], [[JavaScript Bitwise]], [[JavaScript Data Types]]
- **참조 맥락:** Referenced when exact large-integer arithmetic is required beyond the Number safe-integer limit.
## 📚 출처 (Sources)
- [S1] W3Schools — JavaScript BigInt — https://www.w3schools.com/js/js_bigint.asp
## 📝 변경 이력 (Change history)
- 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript BigInt" page (Astra wiki-curation, P-Reinforce v3.1 format).