JavaScript DataView setBigInt64() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Instance method

Definition

setBigInt64() writes a signed 64-bit whole number into eight bytes of a DataView. The value must be a BigInt (such as 42n), because 64-bit integers can be larger than a Number can hold exactly. Pass the byte offset, the BigInt and optionally true for little-endian order. Values outside -9223372036854775808n to 9223372036854775807n wrap around.

Remember
const view = new DataView(new ArrayBuffer(8));
view.setBigInt64(0, 42n);     // undefined
view.getBigInt64(0);          // 42n
view.setBigInt64(0, 42);      // TypeError
view.setBigInt64(0, 2n ** 63n);
view.getBigInt64(0);          // -9223372036854775808n

Syntax

Call setBigInt64() on a DataView object with a byte position and the value to write, plus true if you need little-endian order.

JavaScript
dataview.setBigInt64(byteOffset, value)
dataview.setBigInt64(byteOffset, value, littleEndian)
CallWhat it does
view.setBigInt64(byteOffset, value)Converts value to a signed 64-bit integer (-9223372036854775808n to 9223372036854775807n) and writes it into the 8 bytes starting at byteOffset, big-endian. Returns undefined.
view.setBigInt64(byteOffset, value, true)Writes the same 8 bytes in little-endian order, so the least significant byte lands at byteOffset.

Added in ES2020 together with BigInt. Every current browser and runtime has it. Unlike a BigInt64Array, a DataView has no alignment rule: setBigInt64() works at any byte position, even one that is not a multiple of 8.

Library declarations
JavaScript
DataView.prototype.setBigInt64(byteOffset, value)
DataView.prototype.setBigInt64(byteOffset, value, littleEndian)

// returns undefined; writes 8 bytes starting at byteOffset
// value is converted to a signed 64-bit integer (BigInt required)
// needs byteOffset + 8 <= view.byteLength, else RangeError
// littleEndian: false or left out = big-endian

Parameters

ParameterDescription
byteOffsetWhere to start writing, counted in bytes from the start of the view (not of the underlying buffer). It is converted to a whole number, so 1.9 means 1 and "2" means 2. All 8 bytes must fit: byteOffset + 8 may not be larger than view.byteLength. A negative or too-large offset throws a RangeError.
valueThe BigInt to store, such as 42n. A plain Number like 42 throws a TypeError (strings such as "42" and booleans are converted). Values outside -9223372036854775808n to 9223372036854775807n wrap around modulo 264.
littleEndianOptional. Leave it out (or pass false) for big-endian: the most significant of the 8 bytes comes first. Pass true for little-endian: the least significant byte comes first. Any truthy value, such as 1, also means little-endian.

Return Value

Return ValueDescription
undefinedsetBigInt64() gives nothing back. Its result is the 8 bytes it changed inside the buffer; read them back with getBigInt64().

JavaScript Examples

Example 1 — Write a Millisecond Timestamp and Inspect It

Write a time in milliseconds with setBigInt64() and look at the 8 bytes it produced.

JavaScript
const view = new DataView(new ArrayBuffer(8));
view.setBigInt64(0, 1700000000000n);

const hex = Array.from(new Uint8Array(view.buffer), function (b) {
  return b.toString(16).padStart(2, "0");
}).join(" ");
console.log(hex);                          // 00 00 01 8b cf e5 68 00
console.log(String(view.getBigInt64(0)));  // 1700000000000
Try It Yourself

How It Works

  1. A time in milliseconds. 1700000000000n is a moment in November 2023, counted in milliseconds since 1970, and too large for 32 bits.
  2. Eight bytes, big-endian. setBigInt64() writes the most significant byte first, so the dump starts with zero bytes and ends with the small parts.
  3. Read it back exactly. getBigInt64(0) returns the same 1700000000000n, every digit intact.

Example 2 — Convert a Number Before Calling setBigInt64()

Try to write a plain Number, then convert a decimal value to a BigInt the safe way.

JavaScript
const view = new DataView(new ArrayBuffer(8));

try {
  view.setBigInt64(0, 5);
} catch (e) {
  console.log(e.name);                     // TypeError
}

view.setBigInt64(0, BigInt(Math.trunc(12.7)));
console.log(String(view.getBigInt64(0)));  // 12
Try It Yourself

How It Works

  1. A Number is refused. setBigInt64(0, 5) throws a TypeError: the 64-bit setters do not convert Numbers automatically.
  2. Convert on purpose. Math.trunc(12.7) drops the fraction, and BigInt(12) turns the result into 12n.
  3. Now the write works. With a real BigInt the value is stored, and reading it back prints 12.

Example 3 — The Largest Signed Value and One More

Write the largest signed 64-bit value, then one more, then -1 in little-endian order.

JavaScript
const view = new DataView(new ArrayBuffer(8));

view.setBigInt64(0, 9223372036854775807n);
console.log(String(view.getBigInt64(0)));   // 9223372036854775807

view.setBigInt64(0, 9223372036854775808n);
console.log(String(view.getBigInt64(0)));   // -9223372036854775808

view.setBigInt64(0, -1n, true);
console.log(view.getUint8(0) + " " + view.getUint8(7));   // 255 255
Try It Yourself

How It Works

  1. The largest signed value. 9223372036854775807n (2 to the power 63, minus 1) still fits and comes back unchanged.
  2. One more wraps to the minimum. 9223372036854775808n does not fit, so it wraps around by 2 to the power 64 and reads back as -9223372036854775808n.
  3. Minus one fills every byte. setBigInt64(0, -1n, true) stores 64 one-bits, so the first and last bytes are both 255.

Browser & Runtime Support

setBigInt64() arrived in ES2020, together with BigInt and BigInt64Array.

✓ ES2020 · Widely available

DataView.prototype.setBigInt64()

Every current browser and runtime supports it. Only very old engines without BigInt lack it.

97% Current browser support
Google Chrome Modern versions · Desktop & Mobile
Full support
Mozilla Firefox Modern versions · Desktop & Mobile
Full support
Apple Safari Modern versions · macOS & iOS
Full support
Microsoft Edge Modern versions · Chromium
Full support
Opera Modern versions
Full support
Samsung Internet Modern versions · Android
Full support
Internet Explorer No BigInt support
Not supported
Node.js Current releases · checked on Node 24
Supported
Deno Current releases
Supported
Bun Current releases
Supported
setBigInt64() All current engines

Bottom line: Safe in today’s browsers. If you must support an engine without BigInt, check typeof BigInt === "function" first.

Frequently Asked Questions

The 64-bit setters only accept BigInt values. Write 5n or BigInt(5) instead of 5.
undefined. The eight bytes it wrote are the result; read them with getBigInt64().
It wraps around modulo 2 to the power 64. For example 2n ** 63n reads back as -9223372036854775808n.
Convert the millisecond number to a BigInt first: view.setBigInt64(0, BigInt(Date.now())).
They write the same 64 bits. setBigInt64() thinks of the range -2 to the power 63 up to 2 to the power 63 minus 1; setBigUint64() thinks of 0 up to 2 to the power 64 minus 1.

Next: setBigUint64()

Write an unsigned 64-bit BigInt, such as a byte counter that never goes negative.

setBigUint64() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful