the [ `
replacer ` ](/en-
US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#the_replacer_parameter)
parameter of ` JSON.stringify ` to serialize BigInt values:

js

    
    
    const replacer = (key, value) =>
      typeof value === "bigint" ? { $bigint: value.toString() } : value;
    
    const data = {
      number: 1,
      big: 18014398509481982n,
    };
    const stringified = JSON.stringify(data, replacer);
    
    console.log(stringified);
    // {"number":1,"big":{"$bigint":"18014398509481982"}}
    

You can then use the [ ` reviver ` ](/en-
US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#using_the_reviver_parameter)
parameter of ` JSON.parse ` to handle them:

js

    
    
    const reviver = (key, value) =>
      value !== null &&
      typeof value === "object" &&
      "$bigint" in value &&
      typeof value.$bigint === "string"
        ? BigInt(value.$bigint)
        : value;
    
    const payload = '{"number":1,"big":{"$bigint":"18014398509481982"}}';
    const parsed = JSON.parse(payload, reviver);
    
    console.log(parsed);
    // { number: 1, big: 18014398509481982n }
    

**Note:** While it's possible to make the replacer of ` JSON.stringify() `
generic and properly serialize BigInt values for all objects as shown above,
the reviver of ` JSON.parse() ` has to be used with caution, because the
serialization is _lossy_ : it's not possible to distinguish between an object
that happens to have a property called ` $bigint ` and an actual BigInt.

In addition, the example above creates an entire object during replacing and
reviving, which may have performance or storage implications for larger
objects containing many BigInts. If you know the shape of the payload, it may
be better to just serialize them as strings and revive them based on the
property key's name instead.

In fact, JSON allows number literals that are arbitrarily long; they just
cannot be parsed to full precision in JavaScript. If you are communicating
with another program in a language that supports longer integers (such as
64-bit integers), and you want to transmit the BigInt as a JSON number instead
of a JSON string, see [ Lossless number serialization ](/en-
US/docs/Web/JavaScript/Reference/Global_Objects/JSON#using_json_numbers) .

###  BigInt coercion

Many built-in operations that expect BigInts first coerce their arguments to
BigInts. [ The operation ](https://tc39.es/ecma262/multipage/abstract-
operations.html#sec-tobigint) can be summarized as follows:

  * BigInts are returned as-is. 
  * [ ` undefined ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined) and [ ` null ` ](/en-US/docs/Web/JavaScript/Reference/Operators/null) throw a [ ` TypeError ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) . 
  * ` true ` turns into ` 1n ` ; ` false ` turns into ` 0n ` . 
  * Strings are converted by parsing them as if they contain an integer literal. Any parsing failure results in a [ ` SyntaxError ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError) . The syntax is a subset of [ string numeric literals ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number#number_coercion) , where decimal points or exponent indicators are not allowed. 
  * [ Numbers ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) throw a [ ` TypeError ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) to prevent unintended implicit coercion causing loss of precision. 
  * [ Symbols ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) throw a [ ` TypeError ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) . 
  * Objects are first [ converted to a primitive ](/en-US/docs/Web/JavaScript/Data_structures#primitive_coercion) by calling their [ ` [@@toPrimitive]() ` ](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toPrimitive) (with ` "number" ` as hint), ` valueOf() ` , and ` toString() ` methods, in that order. The resulting primitive