Movatterモバイル変換


[0]ホーム

URL:


  1. 개발자를 위한 웹 기술
  2. JavaScript
  3. JavaScript 참고서
  4. 표준 내장 객체
  5. BigInt
  6. BigInt.prototype.toString()

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

View in EnglishAlways 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)

매개변수

radixOptional

BigInt 값을 표현하기 위해 사용할 기저를 지정하기 위해 사용하는 2부터 36까지의 정수. 기본 값은 10.

반환 값

명시된BigInt 값을 표현하는 문자열

예외

RangeError

radix가 2 미만이거나 36 초과라면 발생

설명

BigInt 객체는ObjecttoString 메서드를 재정의합니다. 즉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은 JavaScriptNumber 유형에만 나타나는 IEEE 부동소수점 개념입니다.

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

명세서

Specification
ECMAScript® 2026 Language Specification
# sec-bigint.prototype.tostring

브라우저 호환성

같이 보기

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp