This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

BigInt.prototype.toString()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Itโ€™s been available across browsers since โจ2020๋…„ 9์›”โฉ.

BigInt ๊ฐ’์˜ toString() ๋ฉ”์„œ๋“œ๋Š” ์ง€์ •๋œ BigInt ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋’ค์— ์˜ค๋Š” "n"์€ ๋ฌธ์ž์—ด์˜ ์ผ๋ถ€๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.

์‹œ๋„ํ•ด ๋ณด๊ธฐ

console.log(1024n.toString());
// Expected output: "1024"

console.log(1024n.toString(2));
// Expected output: "10000000000"

console.log(1024n.toString(16));
// Expected output: "400"

๊ตฌ๋ฌธ

js
toString()
toString(radix)

๋งค๊ฐœ๋ณ€์ˆ˜

radix Optional

BigInt ๊ฐ’์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•  ๊ธฐ์ €๋ฅผ ์ง€์ •ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” 2๋ถ€ํ„ฐ 36๊นŒ์ง€์˜ ์ •์ˆ˜. ๊ธฐ๋ณธ ๊ฐ’์€ 10.

๋ฐ˜ํ™˜ ๊ฐ’

๋ช…์‹œ๋œ BigInt ๊ฐ’์„ ํ‘œํ˜„ํ•˜๋Š” ๋ฌธ์ž์—ด

์˜ˆ์™ธ

RangeError

radix๊ฐ€ 2 ๋ฏธ๋งŒ์ด๊ฑฐ๋‚˜ 36 ์ดˆ๊ณผ๋ผ๋ฉด ๋ฐœ์ƒ

์„ค๋ช…

BigInt ๊ฐ์ฒด๋Š” Object์˜ toString ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ Object.prototype.toString()๋ฅผ ์ƒ์†๋ฐ›์ง€ ์•Š์Šต๋‹ˆ๋‹ค. BigInt ๊ฐ’์˜ ๊ฒฝ์šฐ toString() ๋ฉ”์„œ๋“œ๋Š” ๊ฐ’์„ ๋ช…์‹œ๋œ ๊ธฐ์ €์— ๋”ฐ๋ผ ํ‘œํ˜„ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๊ธฐ์ €๊ฐ€ 10์„ ์ดˆ๊ณผํ•  ๊ฒฝ์šฐ ์•ŒํŒŒ๋ฒณ ๋ฌธ์ž๋Š” 9 ์ด์ƒ์˜ ์ˆซ์ž๋ฅผ ๊ฐ€๋ฆฌํ‚ต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 16์ง„์ˆ˜์—์„œ๋Š” a๋ถ€ํ„ฐ f๊นŒ์ง€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

๋ช…์‹œ๋œ BigInt ๊ฐ’์ด ์Œ์ˆ˜์ผ ๊ฒฝ์šฐ ๋ถ€ํ˜ธ๋Š” ๋ณด์กด๋ฉ๋‹ˆ๋‹ค. ๊ธฐ์ €๊ฐ€ 2์ผ ๊ฒฝ์šฐ์—๋„ ๋งˆ์ฐฌ๊ฐ€์ง€ ์ž…๋‹ˆ๋‹ค. ๋ฐ˜ํ™˜๋˜๋Š” ๋ฌธ์ž์—ด์€ ์•ž์— -๋ถ€ํ˜ธ๊ฐ€ ๋ถ™์€ ์–‘์˜ 2์ง„ ํ‘œํ˜„์ด๋ฉฐ, BigInt ๊ฐ’์˜ 2์˜ ๋ณด์ˆ˜๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.

toString() ๋ฉ”์„œ๋“œ๋Š” this ๊ฐ’์ด BigInt ์›์‹œ ๊ฐ’ ๋˜๋Š” ๋ž˜ํผ ๊ฐ์ฒด์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” ๋‹ค๋ฅธ this ๊ฐ’์„ ๊ฐ•์ œ๋กœ BigInt ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋ ค ํ•˜์ง€ ์•Š๊ณ  TypeError๋ฅผ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.

BigInt๋Š” [@@toPrimitive]() ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด๊ณผ ๊ฐ™์ด BigInt ๊ฐ์ฒด๊ฐ€ ๋ฌธ์ž์—ด์ด ๊ธฐ๋Œ€๋˜๋Š” ๋ฌธ๋งฅ์—์„œ ์‚ฌ์šฉ๋˜์—ˆ์„ ๋•Œ JavaScript๋Š” toString()๋ฅผ ์ž๋™์ ์œผ๋กœ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ BigInt ์›์‹œ ๊ฐ’์€ toString() ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ๋ฌธ์ž์—ด๋กœ ๊ฐ•์ œ ๋ณ€ํ™˜ํ•˜์ง€ ์•Š๊ณ , ์ดˆ๊ธฐ toString() ๊ตฌํ˜„๊ณผ ๋™์ผํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์ง์ ‘ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค.

js
BigInt.prototype.toString = () => "Overridden";
console.log(`${1n}`); // "1"
console.log(`${Object(1n)}`); // "Overridden"

์˜ˆ์ œ

toString() ์‚ฌ์šฉํ•˜๊ธฐ

js
17n.toString(); // "17"
66n.toString(2); // "1000010"
254n.toString(16); // "fe"
(-10n).toString(2); // "-1010"
(-0xffn).toString(2); // "-11111111"

์Œ์ˆ˜ 0 BigInt

์ •์ˆ˜์—๋Š” ์Œ์ˆ˜ 0์ด ์—†์œผ๋ฏ€๋กœ ์Œ์ˆ˜ 0์˜ BigInt๋Š” ์—†์Šต๋‹ˆ๋‹ค. 0.0์€ JavaScript Number ์œ ํ˜•์—๋งŒ ๋‚˜ํƒ€๋‚˜๋Š” IEEE ๋ถ€๋™์†Œ์ˆ˜์  ๊ฐœ๋…์ž…๋‹ˆ๋‹ค.

js
(-0n).toString(); // "0"
BigInt(-0).toString(); // "0"

๋ช…์„ธ์„œ

Specification
ECMAScriptยฎ 2026 Language Specification
# sec-bigint.prototype.tostring

๋ธŒ๋ผ์šฐ์ € ํ˜ธํ™˜์„ฑ

๊ฐ™์ด ๋ณด๊ธฐ