• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="utf-8">
5  <meta name="viewport" content="width=device-width">
6  <meta name="nodejs.org:node-version" content="v18.18.2">
7  <title>Buffer | Node.js v18.18.2 Documentation</title>
8  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
9  <link rel="stylesheet" href="assets/style.css">
10  <link rel="stylesheet" href="assets/hljs.css">
11  <link rel="canonical" href="https://nodejs.org/api/buffer.html">
12  <script async defer src="assets/api.js" type="text/javascript"></script>
13  <style>@media(max-width:558px){.with-42-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:718px){.with-62-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:646px){.with-53-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style>
14</head>
15<body class="alt apidoc" id="api-section-buffer">
16  <div id="content" class="clearfix">
17    <div id="column2" class="interior">
18      <div id="intro" class="interior">
19        <a href="/" title="Go back to the home page">
20          Node.js
21        </a>
22      </div>
23      <ul>
24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
26</ul>
27<hr class="line">
28<ul>
29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
32<li><a href="buffer.html" class="nav-buffer active">Buffer</a></li>
33<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
38<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
39<li><a href="console.html" class="nav-console">Console</a></li>
40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
45<li><a href="dns.html" class="nav-dns">DNS</a></li>
46<li><a href="domain.html" class="nav-domain">Domain</a></li>
47<li><a href="errors.html" class="nav-errors">Errors</a></li>
48<li><a href="events.html" class="nav-events">Events</a></li>
49<li><a href="fs.html" class="nav-fs">File system</a></li>
50<li><a href="globals.html" class="nav-globals">Globals</a></li>
51<li><a href="http.html" class="nav-http">HTTP</a></li>
52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
53<li><a href="https.html" class="nav-https">HTTPS</a></li>
54<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
55<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
60<li><a href="net.html" class="nav-net">Net</a></li>
61<li><a href="os.html" class="nav-os">OS</a></li>
62<li><a href="path.html" class="nav-path">Path</a></li>
63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
65<li><a href="process.html" class="nav-process">Process</a></li>
66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
68<li><a href="readline.html" class="nav-readline">Readline</a></li>
69<li><a href="repl.html" class="nav-repl">REPL</a></li>
70<li><a href="report.html" class="nav-report">Report</a></li>
71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
72<li><a href="stream.html" class="nav-stream">Stream</a></li>
73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
74<li><a href="test.html" class="nav-test">Test runner</a></li>
75<li><a href="timers.html" class="nav-timers">Timers</a></li>
76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
78<li><a href="tty.html" class="nav-tty">TTY</a></li>
79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
80<li><a href="url.html" class="nav-url">URL</a></li>
81<li><a href="util.html" class="nav-util">Utilities</a></li>
82<li><a href="v8.html" class="nav-v8">V8</a></li>
83<li><a href="vm.html" class="nav-vm">VM</a></li>
84<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
89</ul>
90<hr class="line">
91<ul>
92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
93</ul>
94    </div>
95
96    <div id="column1" data-id="buffer" class="interior">
97      <header class="header">
98        <div class="header-container">
99          <h1>Node.js v18.18.2 documentation</h1>
100          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
101            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
102              <path fill="none" d="M0 0h24v24H0z" />
103              <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/>
104              <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/>
105            </svg>
106            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
107              <path d="M0 0h24v24H0z" fill="none" />
108              <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/>
109            </svg>
110          </button>
111        </div>
112        <div id="gtoc">
113          <ul>
114            <li class="pinned-header">Node.js v18.18.2</li>
115
116    <li class="picker-header">
117      <a href="#">
118        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
119        Table of contents
120      </a>
121
122      <div class="picker"><div class="toc"><ul>
123<li><span class="stability_2"><a href="#buffer">Buffer</a></span>
124<ul>
125<li><a href="#buffers-and-character-encodings">Buffers and character encodings</a></li>
126<li><a href="#buffers-and-typedarrays">Buffers and TypedArrays</a></li>
127<li><a href="#buffers-and-iteration">Buffers and iteration</a></li>
128<li><a href="#class-blob">Class: <code>Blob</code></a>
129<ul>
130<li><a href="#new-bufferblobsources-options"><code>new buffer.Blob([sources[, options]])</code></a></li>
131<li><a href="#blobarraybuffer"><code>blob.arrayBuffer()</code></a></li>
132<li><a href="#blobsize"><code>blob.size</code></a></li>
133<li><a href="#blobslicestart-end-type"><code>blob.slice([start[, end[, type]]])</code></a></li>
134<li><a href="#blobstream"><code>blob.stream()</code></a></li>
135<li><a href="#blobtext"><code>blob.text()</code></a></li>
136<li><a href="#blobtype"><code>blob.type</code></a></li>
137<li><a href="#blob-objects-and-messagechannel"><code>Blob</code> objects and <code>MessageChannel</code></a></li>
138</ul>
139</li>
140<li><a href="#class-buffer">Class: <code>Buffer</code></a>
141<ul>
142<li><a href="#static-method-bufferallocsize-fill-encoding">Static method: <code>Buffer.alloc(size[, fill[, encoding]])</code></a></li>
143<li><a href="#static-method-bufferallocunsafesize">Static method: <code>Buffer.allocUnsafe(size)</code></a></li>
144<li><a href="#static-method-bufferallocunsafeslowsize">Static method: <code>Buffer.allocUnsafeSlow(size)</code></a></li>
145<li><a href="#static-method-bufferbytelengthstring-encoding">Static method: <code>Buffer.byteLength(string[, encoding])</code></a></li>
146<li><a href="#static-method-buffercomparebuf1-buf2">Static method: <code>Buffer.compare(buf1, buf2)</code></a></li>
147<li><a href="#static-method-bufferconcatlist-totallength">Static method: <code>Buffer.concat(list[, totalLength])</code></a></li>
148<li><a href="#static-method-buffercopybytesfromview-offset-length">Static method: <code>Buffer.copyBytesFrom(view[, offset[, length]])</code></a></li>
149<li><a href="#static-method-bufferfromarray">Static method: <code>Buffer.from(array)</code></a></li>
150<li><a href="#static-method-bufferfromarraybuffer-byteoffset-length">Static method: <code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a></li>
151<li><a href="#static-method-bufferfrombuffer">Static method: <code>Buffer.from(buffer)</code></a></li>
152<li><a href="#static-method-bufferfromobject-offsetorencoding-length">Static method: <code>Buffer.from(object[, offsetOrEncoding[, length]])</code></a></li>
153<li><a href="#static-method-bufferfromstring-encoding">Static method: <code>Buffer.from(string[, encoding])</code></a></li>
154<li><a href="#static-method-bufferisbufferobj">Static method: <code>Buffer.isBuffer(obj)</code></a></li>
155<li><a href="#static-method-bufferisencodingencoding">Static method: <code>Buffer.isEncoding(encoding)</code></a></li>
156<li><a href="#class-property-bufferpoolsize">Class property: <code>Buffer.poolSize</code></a></li>
157<li><a href="#bufindex"><code>buf[index]</code></a></li>
158<li><a href="#bufbuffer"><code>buf.buffer</code></a></li>
159<li><a href="#bufbyteoffset"><code>buf.byteOffset</code></a></li>
160<li><a href="#bufcomparetarget-targetstart-targetend-sourcestart-sourceend"><code>buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])</code></a></li>
161<li><a href="#bufcopytarget-targetstart-sourcestart-sourceend"><code>buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])</code></a></li>
162<li><a href="#bufentries"><code>buf.entries()</code></a></li>
163<li><a href="#bufequalsotherbuffer"><code>buf.equals(otherBuffer)</code></a></li>
164<li><a href="#buffillvalue-offset-end-encoding"><code>buf.fill(value[, offset[, end]][, encoding])</code></a></li>
165<li><a href="#bufincludesvalue-byteoffset-encoding"><code>buf.includes(value[, byteOffset][, encoding])</code></a></li>
166<li><a href="#bufindexofvalue-byteoffset-encoding"><code>buf.indexOf(value[, byteOffset][, encoding])</code></a></li>
167<li><a href="#bufkeys"><code>buf.keys()</code></a></li>
168<li><a href="#buflastindexofvalue-byteoffset-encoding"><code>buf.lastIndexOf(value[, byteOffset][, encoding])</code></a></li>
169<li><a href="#buflength"><code>buf.length</code></a></li>
170<li><span class="stability_0"><a href="#bufparent"><code>buf.parent</code></a></span></li>
171<li><a href="#bufreadbigint64beoffset"><code>buf.readBigInt64BE([offset])</code></a></li>
172<li><a href="#bufreadbigint64leoffset"><code>buf.readBigInt64LE([offset])</code></a></li>
173<li><a href="#bufreadbiguint64beoffset"><code>buf.readBigUInt64BE([offset])</code></a></li>
174<li><a href="#bufreadbiguint64leoffset"><code>buf.readBigUInt64LE([offset])</code></a></li>
175<li><a href="#bufreaddoublebeoffset"><code>buf.readDoubleBE([offset])</code></a></li>
176<li><a href="#bufreaddoubleleoffset"><code>buf.readDoubleLE([offset])</code></a></li>
177<li><a href="#bufreadfloatbeoffset"><code>buf.readFloatBE([offset])</code></a></li>
178<li><a href="#bufreadfloatleoffset"><code>buf.readFloatLE([offset])</code></a></li>
179<li><a href="#bufreadint8offset"><code>buf.readInt8([offset])</code></a></li>
180<li><a href="#bufreadint16beoffset"><code>buf.readInt16BE([offset])</code></a></li>
181<li><a href="#bufreadint16leoffset"><code>buf.readInt16LE([offset])</code></a></li>
182<li><a href="#bufreadint32beoffset"><code>buf.readInt32BE([offset])</code></a></li>
183<li><a href="#bufreadint32leoffset"><code>buf.readInt32LE([offset])</code></a></li>
184<li><a href="#bufreadintbeoffset-bytelength"><code>buf.readIntBE(offset, byteLength)</code></a></li>
185<li><a href="#bufreadintleoffset-bytelength"><code>buf.readIntLE(offset, byteLength)</code></a></li>
186<li><a href="#bufreaduint8offset"><code>buf.readUInt8([offset])</code></a></li>
187<li><a href="#bufreaduint16beoffset"><code>buf.readUInt16BE([offset])</code></a></li>
188<li><a href="#bufreaduint16leoffset"><code>buf.readUInt16LE([offset])</code></a></li>
189<li><a href="#bufreaduint32beoffset"><code>buf.readUInt32BE([offset])</code></a></li>
190<li><a href="#bufreaduint32leoffset"><code>buf.readUInt32LE([offset])</code></a></li>
191<li><a href="#bufreaduintbeoffset-bytelength"><code>buf.readUIntBE(offset, byteLength)</code></a></li>
192<li><a href="#bufreaduintleoffset-bytelength"><code>buf.readUIntLE(offset, byteLength)</code></a></li>
193<li><a href="#bufsubarraystart-end"><code>buf.subarray([start[, end]])</code></a></li>
194<li><span class="stability_0"><a href="#bufslicestart-end"><code>buf.slice([start[, end]])</code></a></span></li>
195<li><a href="#bufswap16"><code>buf.swap16()</code></a></li>
196<li><a href="#bufswap32"><code>buf.swap32()</code></a></li>
197<li><a href="#bufswap64"><code>buf.swap64()</code></a></li>
198<li><a href="#buftojson"><code>buf.toJSON()</code></a></li>
199<li><a href="#buftostringencoding-start-end"><code>buf.toString([encoding[, start[, end]]])</code></a></li>
200<li><a href="#bufvalues"><code>buf.values()</code></a></li>
201<li><a href="#bufwritestring-offset-length-encoding"><code>buf.write(string[, offset[, length]][, encoding])</code></a></li>
202<li><a href="#bufwritebigint64bevalue-offset"><code>buf.writeBigInt64BE(value[, offset])</code></a></li>
203<li><a href="#bufwritebigint64levalue-offset"><code>buf.writeBigInt64LE(value[, offset])</code></a></li>
204<li><a href="#bufwritebiguint64bevalue-offset"><code>buf.writeBigUInt64BE(value[, offset])</code></a></li>
205<li><a href="#bufwritebiguint64levalue-offset"><code>buf.writeBigUInt64LE(value[, offset])</code></a></li>
206<li><a href="#bufwritedoublebevalue-offset"><code>buf.writeDoubleBE(value[, offset])</code></a></li>
207<li><a href="#bufwritedoublelevalue-offset"><code>buf.writeDoubleLE(value[, offset])</code></a></li>
208<li><a href="#bufwritefloatbevalue-offset"><code>buf.writeFloatBE(value[, offset])</code></a></li>
209<li><a href="#bufwritefloatlevalue-offset"><code>buf.writeFloatLE(value[, offset])</code></a></li>
210<li><a href="#bufwriteint8value-offset"><code>buf.writeInt8(value[, offset])</code></a></li>
211<li><a href="#bufwriteint16bevalue-offset"><code>buf.writeInt16BE(value[, offset])</code></a></li>
212<li><a href="#bufwriteint16levalue-offset"><code>buf.writeInt16LE(value[, offset])</code></a></li>
213<li><a href="#bufwriteint32bevalue-offset"><code>buf.writeInt32BE(value[, offset])</code></a></li>
214<li><a href="#bufwriteint32levalue-offset"><code>buf.writeInt32LE(value[, offset])</code></a></li>
215<li><a href="#bufwriteintbevalue-offset-bytelength"><code>buf.writeIntBE(value, offset, byteLength)</code></a></li>
216<li><a href="#bufwriteintlevalue-offset-bytelength"><code>buf.writeIntLE(value, offset, byteLength)</code></a></li>
217<li><a href="#bufwriteuint8value-offset"><code>buf.writeUInt8(value[, offset])</code></a></li>
218<li><a href="#bufwriteuint16bevalue-offset"><code>buf.writeUInt16BE(value[, offset])</code></a></li>
219<li><a href="#bufwriteuint16levalue-offset"><code>buf.writeUInt16LE(value[, offset])</code></a></li>
220<li><a href="#bufwriteuint32bevalue-offset"><code>buf.writeUInt32BE(value[, offset])</code></a></li>
221<li><a href="#bufwriteuint32levalue-offset"><code>buf.writeUInt32LE(value[, offset])</code></a></li>
222<li><a href="#bufwriteuintbevalue-offset-bytelength"><code>buf.writeUIntBE(value, offset, byteLength)</code></a></li>
223<li><a href="#bufwriteuintlevalue-offset-bytelength"><code>buf.writeUIntLE(value, offset, byteLength)</code></a></li>
224<li><span class="stability_0"><a href="#new-bufferarray"><code>new Buffer(array)</code></a></span></li>
225<li><span class="stability_0"><a href="#new-bufferarraybuffer-byteoffset-length"><code>new Buffer(arrayBuffer[, byteOffset[, length]])</code></a></span></li>
226<li><span class="stability_0"><a href="#new-bufferbuffer"><code>new Buffer(buffer)</code></a></span></li>
227<li><span class="stability_0"><a href="#new-buffersize"><code>new Buffer(size)</code></a></span></li>
228<li><span class="stability_0"><a href="#new-bufferstring-encoding"><code>new Buffer(string[, encoding])</code></a></span></li>
229</ul>
230</li>
231<li><span class="stability_1"><a href="#class-file">Class: <code>File</code></a></span>
232<ul>
233<li><a href="#new-bufferfilesources-filename-options"><code>new buffer.File(sources, fileName[, options])</code></a></li>
234<li><a href="#filename"><code>file.name</code></a></li>
235<li><a href="#filelastmodified"><code>file.lastModified</code></a></li>
236</ul>
237</li>
238<li><a href="#nodebuffer-module-apis"><code>node:buffer</code> module APIs</a>
239<ul>
240<li><span class="stability_3"><a href="#bufferatobdata"><code>buffer.atob(data)</code></a></span></li>
241<li><span class="stability_3"><a href="#bufferbtoadata"><code>buffer.btoa(data)</code></a></span></li>
242<li><a href="#bufferisasciiinput"><code>buffer.isAscii(input)</code></a></li>
243<li><a href="#bufferisutf8input"><code>buffer.isUtf8(input)</code></a></li>
244<li><a href="#bufferinspect_max_bytes"><code>buffer.INSPECT_MAX_BYTES</code></a></li>
245<li><a href="#bufferkmaxlength"><code>buffer.kMaxLength</code></a></li>
246<li><a href="#bufferkstringmaxlength"><code>buffer.kStringMaxLength</code></a></li>
247<li><span class="stability_1"><a href="#bufferresolveobjecturlid"><code>buffer.resolveObjectURL(id)</code></a></span></li>
248<li><a href="#buffertranscodesource-fromenc-toenc"><code>buffer.transcode(source, fromEnc, toEnc)</code></a></li>
249<li><span class="stability_0"><a href="#class-slowbuffer">Class: <code>SlowBuffer</code></a></span>
250<ul>
251<li><span class="stability_0"><a href="#new-slowbuffersize"><code>new SlowBuffer(size)</code></a></span></li>
252</ul>
253</li>
254<li><a href="#buffer-constants">Buffer constants</a>
255<ul>
256<li><a href="#bufferconstantsmax_length"><code>buffer.constants.MAX_LENGTH</code></a></li>
257<li><a href="#bufferconstantsmax_string_length"><code>buffer.constants.MAX_STRING_LENGTH</code></a></li>
258</ul>
259</li>
260</ul>
261</li>
262<li><a href="#bufferfrom-bufferalloc-and-bufferallocunsafe"><code>Buffer.from()</code>, <code>Buffer.alloc()</code>, and <code>Buffer.allocUnsafe()</code></a>
263<ul>
264<li><a href="#the---zero-fill-buffers-command-line-option">The <code>--zero-fill-buffers</code> command-line option</a></li>
265<li><a href="#what-makes-bufferallocunsafe-and-bufferallocunsafeslow-unsafe">What makes <code>Buffer.allocUnsafe()</code> and <code>Buffer.allocUnsafeSlow()</code> "unsafe"?</a></li>
266</ul>
267</li>
268</ul>
269</li>
270</ul></div></div>
271    </li>
272
273
274    <li class="picker-header">
275      <a href="#">
276        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
277        Index
278      </a>
279
280      <div class="picker"><ul>
281<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
282<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
283
284      <li>
285        <a href="index.html">Index</a>
286      </li>
287    </ul>
288
289<hr class="line">
290<ul>
291<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
292<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
293<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
294<li><a href="buffer.html" class="nav-buffer active">Buffer</a></li>
295<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
296<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
297<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
298<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
299<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
300<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
301<li><a href="console.html" class="nav-console">Console</a></li>
302<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
303<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
304<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
305<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
306<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
307<li><a href="dns.html" class="nav-dns">DNS</a></li>
308<li><a href="domain.html" class="nav-domain">Domain</a></li>
309<li><a href="errors.html" class="nav-errors">Errors</a></li>
310<li><a href="events.html" class="nav-events">Events</a></li>
311<li><a href="fs.html" class="nav-fs">File system</a></li>
312<li><a href="globals.html" class="nav-globals">Globals</a></li>
313<li><a href="http.html" class="nav-http">HTTP</a></li>
314<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
315<li><a href="https.html" class="nav-https">HTTPS</a></li>
316<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
317<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
318<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
319<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
320<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
321<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
322<li><a href="net.html" class="nav-net">Net</a></li>
323<li><a href="os.html" class="nav-os">OS</a></li>
324<li><a href="path.html" class="nav-path">Path</a></li>
325<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
326<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
327<li><a href="process.html" class="nav-process">Process</a></li>
328<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
329<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
330<li><a href="readline.html" class="nav-readline">Readline</a></li>
331<li><a href="repl.html" class="nav-repl">REPL</a></li>
332<li><a href="report.html" class="nav-report">Report</a></li>
333<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
334<li><a href="stream.html" class="nav-stream">Stream</a></li>
335<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
336<li><a href="test.html" class="nav-test">Test runner</a></li>
337<li><a href="timers.html" class="nav-timers">Timers</a></li>
338<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
339<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
340<li><a href="tty.html" class="nav-tty">TTY</a></li>
341<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
342<li><a href="url.html" class="nav-url">URL</a></li>
343<li><a href="util.html" class="nav-util">Utilities</a></li>
344<li><a href="v8.html" class="nav-v8">V8</a></li>
345<li><a href="vm.html" class="nav-vm">VM</a></li>
346<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
347<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
348<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
349<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
350<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
351</ul>
352<hr class="line">
353<ul>
354<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
355</ul></div>
356    </li>
357
358
359    <li class="picker-header">
360      <a href="#">
361        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
362        Other versions
363      </a>
364      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v20.x/api/buffer.html">20.x</a></li>
365<li><a href="https://nodejs.org/docs/latest-v19.x/api/buffer.html">19.x</a></li>
366<li><a href="https://nodejs.org/docs/latest-v18.x/api/buffer.html">18.x <b>LTS</b></a></li>
367<li><a href="https://nodejs.org/docs/latest-v17.x/api/buffer.html">17.x</a></li>
368<li><a href="https://nodejs.org/docs/latest-v16.x/api/buffer.html">16.x</a></li>
369<li><a href="https://nodejs.org/docs/latest-v15.x/api/buffer.html">15.x</a></li>
370<li><a href="https://nodejs.org/docs/latest-v14.x/api/buffer.html">14.x</a></li>
371<li><a href="https://nodejs.org/docs/latest-v13.x/api/buffer.html">13.x</a></li>
372<li><a href="https://nodejs.org/docs/latest-v12.x/api/buffer.html">12.x</a></li>
373<li><a href="https://nodejs.org/docs/latest-v11.x/api/buffer.html">11.x</a></li>
374<li><a href="https://nodejs.org/docs/latest-v10.x/api/buffer.html">10.x</a></li>
375<li><a href="https://nodejs.org/docs/latest-v9.x/api/buffer.html">9.x</a></li>
376<li><a href="https://nodejs.org/docs/latest-v8.x/api/buffer.html">8.x</a></li>
377<li><a href="https://nodejs.org/docs/latest-v7.x/api/buffer.html">7.x</a></li>
378<li><a href="https://nodejs.org/docs/latest-v6.x/api/buffer.html">6.x</a></li>
379<li><a href="https://nodejs.org/docs/latest-v5.x/api/buffer.html">5.x</a></li>
380<li><a href="https://nodejs.org/docs/latest-v4.x/api/buffer.html">4.x</a></li>
381<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/buffer.html">0.12.x</a></li>
382<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/buffer.html">0.10.x</a></li></ol></div>
383    </li>
384
385            <li class="picker-header">
386              <a href="#">
387                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
388                Options
389              </a>
390
391              <div class="picker">
392                <ul>
393                  <li>
394                    <a href="all.html">View on single page</a>
395                  </li>
396                  <li>
397                    <a href="buffer.json">View as JSON</a>
398                  </li>
399                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/buffer.md">Edit on GitHub</a></li>
400                </ul>
401              </div>
402            </li>
403          </ul>
404        </div>
405        <hr>
406      </header>
407
408      <details id="toc" open><summary>Table of contents</summary><ul>
409<li><span class="stability_2"><a href="#buffer">Buffer</a></span>
410<ul>
411<li><a href="#buffers-and-character-encodings">Buffers and character encodings</a></li>
412<li><a href="#buffers-and-typedarrays">Buffers and TypedArrays</a></li>
413<li><a href="#buffers-and-iteration">Buffers and iteration</a></li>
414<li><a href="#class-blob">Class: <code>Blob</code></a>
415<ul>
416<li><a href="#new-bufferblobsources-options"><code>new buffer.Blob([sources[, options]])</code></a></li>
417<li><a href="#blobarraybuffer"><code>blob.arrayBuffer()</code></a></li>
418<li><a href="#blobsize"><code>blob.size</code></a></li>
419<li><a href="#blobslicestart-end-type"><code>blob.slice([start[, end[, type]]])</code></a></li>
420<li><a href="#blobstream"><code>blob.stream()</code></a></li>
421<li><a href="#blobtext"><code>blob.text()</code></a></li>
422<li><a href="#blobtype"><code>blob.type</code></a></li>
423<li><a href="#blob-objects-and-messagechannel"><code>Blob</code> objects and <code>MessageChannel</code></a></li>
424</ul>
425</li>
426<li><a href="#class-buffer">Class: <code>Buffer</code></a>
427<ul>
428<li><a href="#static-method-bufferallocsize-fill-encoding">Static method: <code>Buffer.alloc(size[, fill[, encoding]])</code></a></li>
429<li><a href="#static-method-bufferallocunsafesize">Static method: <code>Buffer.allocUnsafe(size)</code></a></li>
430<li><a href="#static-method-bufferallocunsafeslowsize">Static method: <code>Buffer.allocUnsafeSlow(size)</code></a></li>
431<li><a href="#static-method-bufferbytelengthstring-encoding">Static method: <code>Buffer.byteLength(string[, encoding])</code></a></li>
432<li><a href="#static-method-buffercomparebuf1-buf2">Static method: <code>Buffer.compare(buf1, buf2)</code></a></li>
433<li><a href="#static-method-bufferconcatlist-totallength">Static method: <code>Buffer.concat(list[, totalLength])</code></a></li>
434<li><a href="#static-method-buffercopybytesfromview-offset-length">Static method: <code>Buffer.copyBytesFrom(view[, offset[, length]])</code></a></li>
435<li><a href="#static-method-bufferfromarray">Static method: <code>Buffer.from(array)</code></a></li>
436<li><a href="#static-method-bufferfromarraybuffer-byteoffset-length">Static method: <code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a></li>
437<li><a href="#static-method-bufferfrombuffer">Static method: <code>Buffer.from(buffer)</code></a></li>
438<li><a href="#static-method-bufferfromobject-offsetorencoding-length">Static method: <code>Buffer.from(object[, offsetOrEncoding[, length]])</code></a></li>
439<li><a href="#static-method-bufferfromstring-encoding">Static method: <code>Buffer.from(string[, encoding])</code></a></li>
440<li><a href="#static-method-bufferisbufferobj">Static method: <code>Buffer.isBuffer(obj)</code></a></li>
441<li><a href="#static-method-bufferisencodingencoding">Static method: <code>Buffer.isEncoding(encoding)</code></a></li>
442<li><a href="#class-property-bufferpoolsize">Class property: <code>Buffer.poolSize</code></a></li>
443<li><a href="#bufindex"><code>buf[index]</code></a></li>
444<li><a href="#bufbuffer"><code>buf.buffer</code></a></li>
445<li><a href="#bufbyteoffset"><code>buf.byteOffset</code></a></li>
446<li><a href="#bufcomparetarget-targetstart-targetend-sourcestart-sourceend"><code>buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])</code></a></li>
447<li><a href="#bufcopytarget-targetstart-sourcestart-sourceend"><code>buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])</code></a></li>
448<li><a href="#bufentries"><code>buf.entries()</code></a></li>
449<li><a href="#bufequalsotherbuffer"><code>buf.equals(otherBuffer)</code></a></li>
450<li><a href="#buffillvalue-offset-end-encoding"><code>buf.fill(value[, offset[, end]][, encoding])</code></a></li>
451<li><a href="#bufincludesvalue-byteoffset-encoding"><code>buf.includes(value[, byteOffset][, encoding])</code></a></li>
452<li><a href="#bufindexofvalue-byteoffset-encoding"><code>buf.indexOf(value[, byteOffset][, encoding])</code></a></li>
453<li><a href="#bufkeys"><code>buf.keys()</code></a></li>
454<li><a href="#buflastindexofvalue-byteoffset-encoding"><code>buf.lastIndexOf(value[, byteOffset][, encoding])</code></a></li>
455<li><a href="#buflength"><code>buf.length</code></a></li>
456<li><span class="stability_0"><a href="#bufparent"><code>buf.parent</code></a></span></li>
457<li><a href="#bufreadbigint64beoffset"><code>buf.readBigInt64BE([offset])</code></a></li>
458<li><a href="#bufreadbigint64leoffset"><code>buf.readBigInt64LE([offset])</code></a></li>
459<li><a href="#bufreadbiguint64beoffset"><code>buf.readBigUInt64BE([offset])</code></a></li>
460<li><a href="#bufreadbiguint64leoffset"><code>buf.readBigUInt64LE([offset])</code></a></li>
461<li><a href="#bufreaddoublebeoffset"><code>buf.readDoubleBE([offset])</code></a></li>
462<li><a href="#bufreaddoubleleoffset"><code>buf.readDoubleLE([offset])</code></a></li>
463<li><a href="#bufreadfloatbeoffset"><code>buf.readFloatBE([offset])</code></a></li>
464<li><a href="#bufreadfloatleoffset"><code>buf.readFloatLE([offset])</code></a></li>
465<li><a href="#bufreadint8offset"><code>buf.readInt8([offset])</code></a></li>
466<li><a href="#bufreadint16beoffset"><code>buf.readInt16BE([offset])</code></a></li>
467<li><a href="#bufreadint16leoffset"><code>buf.readInt16LE([offset])</code></a></li>
468<li><a href="#bufreadint32beoffset"><code>buf.readInt32BE([offset])</code></a></li>
469<li><a href="#bufreadint32leoffset"><code>buf.readInt32LE([offset])</code></a></li>
470<li><a href="#bufreadintbeoffset-bytelength"><code>buf.readIntBE(offset, byteLength)</code></a></li>
471<li><a href="#bufreadintleoffset-bytelength"><code>buf.readIntLE(offset, byteLength)</code></a></li>
472<li><a href="#bufreaduint8offset"><code>buf.readUInt8([offset])</code></a></li>
473<li><a href="#bufreaduint16beoffset"><code>buf.readUInt16BE([offset])</code></a></li>
474<li><a href="#bufreaduint16leoffset"><code>buf.readUInt16LE([offset])</code></a></li>
475<li><a href="#bufreaduint32beoffset"><code>buf.readUInt32BE([offset])</code></a></li>
476<li><a href="#bufreaduint32leoffset"><code>buf.readUInt32LE([offset])</code></a></li>
477<li><a href="#bufreaduintbeoffset-bytelength"><code>buf.readUIntBE(offset, byteLength)</code></a></li>
478<li><a href="#bufreaduintleoffset-bytelength"><code>buf.readUIntLE(offset, byteLength)</code></a></li>
479<li><a href="#bufsubarraystart-end"><code>buf.subarray([start[, end]])</code></a></li>
480<li><span class="stability_0"><a href="#bufslicestart-end"><code>buf.slice([start[, end]])</code></a></span></li>
481<li><a href="#bufswap16"><code>buf.swap16()</code></a></li>
482<li><a href="#bufswap32"><code>buf.swap32()</code></a></li>
483<li><a href="#bufswap64"><code>buf.swap64()</code></a></li>
484<li><a href="#buftojson"><code>buf.toJSON()</code></a></li>
485<li><a href="#buftostringencoding-start-end"><code>buf.toString([encoding[, start[, end]]])</code></a></li>
486<li><a href="#bufvalues"><code>buf.values()</code></a></li>
487<li><a href="#bufwritestring-offset-length-encoding"><code>buf.write(string[, offset[, length]][, encoding])</code></a></li>
488<li><a href="#bufwritebigint64bevalue-offset"><code>buf.writeBigInt64BE(value[, offset])</code></a></li>
489<li><a href="#bufwritebigint64levalue-offset"><code>buf.writeBigInt64LE(value[, offset])</code></a></li>
490<li><a href="#bufwritebiguint64bevalue-offset"><code>buf.writeBigUInt64BE(value[, offset])</code></a></li>
491<li><a href="#bufwritebiguint64levalue-offset"><code>buf.writeBigUInt64LE(value[, offset])</code></a></li>
492<li><a href="#bufwritedoublebevalue-offset"><code>buf.writeDoubleBE(value[, offset])</code></a></li>
493<li><a href="#bufwritedoublelevalue-offset"><code>buf.writeDoubleLE(value[, offset])</code></a></li>
494<li><a href="#bufwritefloatbevalue-offset"><code>buf.writeFloatBE(value[, offset])</code></a></li>
495<li><a href="#bufwritefloatlevalue-offset"><code>buf.writeFloatLE(value[, offset])</code></a></li>
496<li><a href="#bufwriteint8value-offset"><code>buf.writeInt8(value[, offset])</code></a></li>
497<li><a href="#bufwriteint16bevalue-offset"><code>buf.writeInt16BE(value[, offset])</code></a></li>
498<li><a href="#bufwriteint16levalue-offset"><code>buf.writeInt16LE(value[, offset])</code></a></li>
499<li><a href="#bufwriteint32bevalue-offset"><code>buf.writeInt32BE(value[, offset])</code></a></li>
500<li><a href="#bufwriteint32levalue-offset"><code>buf.writeInt32LE(value[, offset])</code></a></li>
501<li><a href="#bufwriteintbevalue-offset-bytelength"><code>buf.writeIntBE(value, offset, byteLength)</code></a></li>
502<li><a href="#bufwriteintlevalue-offset-bytelength"><code>buf.writeIntLE(value, offset, byteLength)</code></a></li>
503<li><a href="#bufwriteuint8value-offset"><code>buf.writeUInt8(value[, offset])</code></a></li>
504<li><a href="#bufwriteuint16bevalue-offset"><code>buf.writeUInt16BE(value[, offset])</code></a></li>
505<li><a href="#bufwriteuint16levalue-offset"><code>buf.writeUInt16LE(value[, offset])</code></a></li>
506<li><a href="#bufwriteuint32bevalue-offset"><code>buf.writeUInt32BE(value[, offset])</code></a></li>
507<li><a href="#bufwriteuint32levalue-offset"><code>buf.writeUInt32LE(value[, offset])</code></a></li>
508<li><a href="#bufwriteuintbevalue-offset-bytelength"><code>buf.writeUIntBE(value, offset, byteLength)</code></a></li>
509<li><a href="#bufwriteuintlevalue-offset-bytelength"><code>buf.writeUIntLE(value, offset, byteLength)</code></a></li>
510<li><span class="stability_0"><a href="#new-bufferarray"><code>new Buffer(array)</code></a></span></li>
511<li><span class="stability_0"><a href="#new-bufferarraybuffer-byteoffset-length"><code>new Buffer(arrayBuffer[, byteOffset[, length]])</code></a></span></li>
512<li><span class="stability_0"><a href="#new-bufferbuffer"><code>new Buffer(buffer)</code></a></span></li>
513<li><span class="stability_0"><a href="#new-buffersize"><code>new Buffer(size)</code></a></span></li>
514<li><span class="stability_0"><a href="#new-bufferstring-encoding"><code>new Buffer(string[, encoding])</code></a></span></li>
515</ul>
516</li>
517<li><span class="stability_1"><a href="#class-file">Class: <code>File</code></a></span>
518<ul>
519<li><a href="#new-bufferfilesources-filename-options"><code>new buffer.File(sources, fileName[, options])</code></a></li>
520<li><a href="#filename"><code>file.name</code></a></li>
521<li><a href="#filelastmodified"><code>file.lastModified</code></a></li>
522</ul>
523</li>
524<li><a href="#nodebuffer-module-apis"><code>node:buffer</code> module APIs</a>
525<ul>
526<li><span class="stability_3"><a href="#bufferatobdata"><code>buffer.atob(data)</code></a></span></li>
527<li><span class="stability_3"><a href="#bufferbtoadata"><code>buffer.btoa(data)</code></a></span></li>
528<li><a href="#bufferisasciiinput"><code>buffer.isAscii(input)</code></a></li>
529<li><a href="#bufferisutf8input"><code>buffer.isUtf8(input)</code></a></li>
530<li><a href="#bufferinspect_max_bytes"><code>buffer.INSPECT_MAX_BYTES</code></a></li>
531<li><a href="#bufferkmaxlength"><code>buffer.kMaxLength</code></a></li>
532<li><a href="#bufferkstringmaxlength"><code>buffer.kStringMaxLength</code></a></li>
533<li><span class="stability_1"><a href="#bufferresolveobjecturlid"><code>buffer.resolveObjectURL(id)</code></a></span></li>
534<li><a href="#buffertranscodesource-fromenc-toenc"><code>buffer.transcode(source, fromEnc, toEnc)</code></a></li>
535<li><span class="stability_0"><a href="#class-slowbuffer">Class: <code>SlowBuffer</code></a></span>
536<ul>
537<li><span class="stability_0"><a href="#new-slowbuffersize"><code>new SlowBuffer(size)</code></a></span></li>
538</ul>
539</li>
540<li><a href="#buffer-constants">Buffer constants</a>
541<ul>
542<li><a href="#bufferconstantsmax_length"><code>buffer.constants.MAX_LENGTH</code></a></li>
543<li><a href="#bufferconstantsmax_string_length"><code>buffer.constants.MAX_STRING_LENGTH</code></a></li>
544</ul>
545</li>
546</ul>
547</li>
548<li><a href="#bufferfrom-bufferalloc-and-bufferallocunsafe"><code>Buffer.from()</code>, <code>Buffer.alloc()</code>, and <code>Buffer.allocUnsafe()</code></a>
549<ul>
550<li><a href="#the---zero-fill-buffers-command-line-option">The <code>--zero-fill-buffers</code> command-line option</a></li>
551<li><a href="#what-makes-bufferallocunsafe-and-bufferallocunsafeslow-unsafe">What makes <code>Buffer.allocUnsafe()</code> and <code>Buffer.allocUnsafeSlow()</code> "unsafe"?</a></li>
552</ul>
553</li>
554</ul>
555</li>
556</ul></details>
557
558      <div id="apicontent">
559        <h2>Buffer<span><a class="mark" href="#buffer" id="buffer">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer"></a></h2>
560
561<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p>
562<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.18.2/lib/buffer.js">lib/buffer.js</a></p>
563<p><code>Buffer</code> objects are used to represent a fixed-length sequence of bytes. Many
564Node.js APIs support <code>Buffer</code>s.</p>
565<p>The <code>Buffer</code> class is a subclass of JavaScript's <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> class and
566extends it with methods that cover additional use cases. Node.js APIs accept
567plain <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a>s wherever <code>Buffer</code>s are supported as well.</p>
568<p>While the <code>Buffer</code> class is available within the global scope, it is still
569recommended to explicitly reference it via an import or require statement.</p>
570
571<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
572
573<span class="hljs-comment">// Creates a zero-filled Buffer of length 10.</span>
574<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">10</span>);
575
576<span class="hljs-comment">// Creates a Buffer of length 10,</span>
577<span class="hljs-comment">// filled with bytes which all have the value `1`.</span>
578<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">10</span>, <span class="hljs-number">1</span>);
579
580<span class="hljs-comment">// Creates an uninitialized buffer of length 10.</span>
581<span class="hljs-comment">// This is faster than calling Buffer.alloc() but the returned</span>
582<span class="hljs-comment">// Buffer instance might contain old data that needs to be</span>
583<span class="hljs-comment">// overwritten using fill(), write(), or other functions that fill the Buffer's</span>
584<span class="hljs-comment">// contents.</span>
585<span class="hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">10</span>);
586
587<span class="hljs-comment">// Creates a Buffer containing the bytes [1, 2, 3].</span>
588<span class="hljs-keyword">const</span> buf4 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]);
589
590<span class="hljs-comment">// Creates a Buffer containing the bytes [1, 1, 1, 1] – the entries</span>
591<span class="hljs-comment">// are all truncated using `(value &#x26; 255)` to fit into the range 0–255.</span>
592<span class="hljs-keyword">const</span> buf5 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">257</span>, <span class="hljs-number">257.5</span>, -<span class="hljs-number">255</span>, <span class="hljs-string">'1'</span>]);
593
594<span class="hljs-comment">// Creates a Buffer containing the UTF-8-encoded bytes for the string 'tést':</span>
595<span class="hljs-comment">// [0x74, 0xc3, 0xa9, 0x73, 0x74] (in hexadecimal notation)</span>
596<span class="hljs-comment">// [116, 195, 169, 115, 116] (in decimal notation)</span>
597<span class="hljs-keyword">const</span> buf6 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'tést'</span>);
598
599<span class="hljs-comment">// Creates a Buffer containing the Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].</span>
600<span class="hljs-keyword">const</span> buf7 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'tést'</span>, <span class="hljs-string">'latin1'</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
601
602<span class="hljs-comment">// Creates a zero-filled Buffer of length 10.</span>
603<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">10</span>);
604
605<span class="hljs-comment">// Creates a Buffer of length 10,</span>
606<span class="hljs-comment">// filled with bytes which all have the value `1`.</span>
607<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">10</span>, <span class="hljs-number">1</span>);
608
609<span class="hljs-comment">// Creates an uninitialized buffer of length 10.</span>
610<span class="hljs-comment">// This is faster than calling Buffer.alloc() but the returned</span>
611<span class="hljs-comment">// Buffer instance might contain old data that needs to be</span>
612<span class="hljs-comment">// overwritten using fill(), write(), or other functions that fill the Buffer's</span>
613<span class="hljs-comment">// contents.</span>
614<span class="hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">10</span>);
615
616<span class="hljs-comment">// Creates a Buffer containing the bytes [1, 2, 3].</span>
617<span class="hljs-keyword">const</span> buf4 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]);
618
619<span class="hljs-comment">// Creates a Buffer containing the bytes [1, 1, 1, 1] – the entries</span>
620<span class="hljs-comment">// are all truncated using `(value &#x26; 255)` to fit into the range 0–255.</span>
621<span class="hljs-keyword">const</span> buf5 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">257</span>, <span class="hljs-number">257.5</span>, -<span class="hljs-number">255</span>, <span class="hljs-string">'1'</span>]);
622
623<span class="hljs-comment">// Creates a Buffer containing the UTF-8-encoded bytes for the string 'tést':</span>
624<span class="hljs-comment">// [0x74, 0xc3, 0xa9, 0x73, 0x74] (in hexadecimal notation)</span>
625<span class="hljs-comment">// [116, 195, 169, 115, 116] (in decimal notation)</span>
626<span class="hljs-keyword">const</span> buf6 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'tést'</span>);
627
628<span class="hljs-comment">// Creates a Buffer containing the Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].</span>
629<span class="hljs-keyword">const</span> buf7 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'tést'</span>, <span class="hljs-string">'latin1'</span>);</code><button class="copy-button">copy</button></pre>
630<section><h3>Buffers and character encodings<span><a class="mark" href="#buffers-and-character-encodings" id="buffers-and-character-encodings">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffers_and_character_encodings"></a></h3>
631<div class="api_metadata">
632<details class="changelog"><summary>History</summary>
633<table>
634<tbody><tr><th>Version</th><th>Changes</th></tr>
635<tr><td>v15.7.0, v14.18.0</td>
636<td><p>Introduced <code>base64url</code> encoding.</p></td></tr>
637<tr><td>v6.4.0</td>
638<td><p>Introduced <code>latin1</code> as an alias for <code>binary</code>.</p></td></tr>
639<tr><td>v5.0.0</td>
640<td><p>Removed the deprecated <code>raw</code> and <code>raws</code> encodings.</p></td></tr>
641</tbody></table>
642</details>
643</div>
644<p>When converting between <code>Buffer</code>s and strings, a character encoding may be
645specified. If no character encoding is specified, UTF-8 will be used as the
646default.</p>
647
648<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
649
650<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'hello world'</span>, <span class="hljs-string">'utf8'</span>);
651
652<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
653<span class="hljs-comment">// Prints: 68656c6c6f20776f726c64</span>
654<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'base64'</span>));
655<span class="hljs-comment">// Prints: aGVsbG8gd29ybGQ=</span>
656
657<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'fhqwhgads'</span>, <span class="hljs-string">'utf8'</span>));
658<span class="hljs-comment">// Prints: &#x3C;Buffer 66 68 71 77 68 67 61 64 73></span>
659<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'fhqwhgads'</span>, <span class="hljs-string">'utf16le'</span>));
660<span class="hljs-comment">// Prints: &#x3C;Buffer 66 00 68 00 71 00 77 00 68 00 67 00 61 00 64 00 73 00></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
661
662<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'hello world'</span>, <span class="hljs-string">'utf8'</span>);
663
664<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
665<span class="hljs-comment">// Prints: 68656c6c6f20776f726c64</span>
666<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'base64'</span>));
667<span class="hljs-comment">// Prints: aGVsbG8gd29ybGQ=</span>
668
669<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'fhqwhgads'</span>, <span class="hljs-string">'utf8'</span>));
670<span class="hljs-comment">// Prints: &#x3C;Buffer 66 68 71 77 68 67 61 64 73></span>
671<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'fhqwhgads'</span>, <span class="hljs-string">'utf16le'</span>));
672<span class="hljs-comment">// Prints: &#x3C;Buffer 66 00 68 00 71 00 77 00 68 00 67 00 61 00 64 00 73 00></span></code><button class="copy-button">copy</button></pre>
673<p>Node.js buffers accept all case variations of encoding strings that they
674receive. For example, UTF-8 can be specified as <code>'utf8'</code>, <code>'UTF8'</code>, or <code>'uTf8'</code>.</p>
675<p>The character encodings currently supported by Node.js are the following:</p>
676<ul>
677<li>
678<p><code>'utf8'</code> (alias: <code>'utf-8'</code>): Multi-byte encoded Unicode characters. Many web
679pages and other document formats use <a href="https://en.wikipedia.org/wiki/UTF-8">UTF-8</a>. This is the default character
680encoding. When decoding a <code>Buffer</code> into a string that does not exclusively
681contain valid UTF-8 data, the Unicode replacement character <code>U+FFFD</code> � will be
682used to represent those errors.</p>
683</li>
684<li>
685<p><code>'utf16le'</code> (alias: <code>'utf-16le'</code>): Multi-byte encoded Unicode characters.
686Unlike <code>'utf8'</code>, each character in the string will be encoded using either 2
687or 4 bytes. Node.js only supports the <a href="https://en.wikipedia.org/wiki/Endianness">little-endian</a> variant of
688<a href="https://en.wikipedia.org/wiki/UTF-16">UTF-16</a>.</p>
689</li>
690<li>
691<p><code>'latin1'</code>: Latin-1 stands for <a href="https://en.wikipedia.org/wiki/ISO-8859-1">ISO-8859-1</a>. This character encoding only
692supports the Unicode characters from <code>U+0000</code> to <code>U+00FF</code>. Each character is
693encoded using a single byte. Characters that do not fit into that range are
694truncated and will be mapped to characters in that range.</p>
695</li>
696</ul>
697<p>Converting a <code>Buffer</code> into a string using one of the above is referred to as
698decoding, and converting a string into a <code>Buffer</code> is referred to as encoding.</p>
699<p>Node.js also supports the following binary-to-text encodings. For
700binary-to-text encodings, the naming convention is reversed: Converting a
701<code>Buffer</code> into a string is typically referred to as encoding, and converting a
702string into a <code>Buffer</code> as decoding.</p>
703<ul>
704<li>
705<p><code>'base64'</code>: <a href="https://en.wikipedia.org/wiki/Base64">Base64</a> encoding. When creating a <code>Buffer</code> from a string,
706this encoding will also correctly accept "URL and Filename Safe Alphabet" as
707specified in <a href="https://tools.ietf.org/html/rfc4648#section-5">RFC 4648, Section 5</a>. Whitespace characters such as spaces,
708tabs, and new lines contained within the base64-encoded string are ignored.</p>
709</li>
710<li>
711<p><code>'base64url'</code>: <a href="https://tools.ietf.org/html/rfc4648#section-5">base64url</a> encoding as specified in
712<a href="https://tools.ietf.org/html/rfc4648#section-5">RFC 4648, Section 5</a>. When creating a <code>Buffer</code> from a string, this
713encoding will also correctly accept regular base64-encoded strings. When
714encoding a <code>Buffer</code> to a string, this encoding will omit padding.</p>
715</li>
716<li>
717<p><code>'hex'</code>: Encode each byte as two hexadecimal characters. Data truncation
718may occur when decoding strings that do not exclusively consist of an even
719number of hexadecimal characters. See below for an example.</p>
720</li>
721</ul>
722<p>The following legacy character encodings are also supported:</p>
723<ul>
724<li>
725<p><code>'ascii'</code>: For 7-bit <a href="https://en.wikipedia.org/wiki/ASCII">ASCII</a> data only. When encoding a string into a
726<code>Buffer</code>, this is equivalent to using <code>'latin1'</code>. When decoding a <code>Buffer</code>
727into a string, using this encoding will additionally unset the highest bit of
728each byte before decoding as <code>'latin1'</code>.
729Generally, there should be no reason to use this encoding, as <code>'utf8'</code>
730(or, if the data is known to always be ASCII-only, <code>'latin1'</code>) will be a
731better choice when encoding or decoding ASCII-only text. It is only provided
732for legacy compatibility.</p>
733</li>
734<li>
735<p><code>'binary'</code>: Alias for <code>'latin1'</code>.
736The name of this encoding can be very misleading, as all of the
737encodings listed here convert between strings and binary data. For converting
738between strings and <code>Buffer</code>s, typically <code>'utf8'</code> is the right choice.</p>
739</li>
740<li>
741<p><code>'ucs2'</code>, <code>'ucs-2'</code>: Aliases of <code>'utf16le'</code>. UCS-2 used to refer to a variant
742of UTF-16 that did not support characters that had code points larger than
743U+FFFF. In Node.js, these code points are always supported.</p>
744</li>
745</ul>
746
747<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
748
749<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'1ag123'</span>, <span class="hljs-string">'hex'</span>);
750<span class="hljs-comment">// Prints &#x3C;Buffer 1a>, data truncated when first non-hexadecimal value</span>
751<span class="hljs-comment">// ('g') encountered.</span>
752
753<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'1a7'</span>, <span class="hljs-string">'hex'</span>);
754<span class="hljs-comment">// Prints &#x3C;Buffer 1a>, data truncated when data ends in single digit ('7').</span>
755
756<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'1634'</span>, <span class="hljs-string">'hex'</span>);
757<span class="hljs-comment">// Prints &#x3C;Buffer 16 34>, all data represented.</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
758
759<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'1ag123'</span>, <span class="hljs-string">'hex'</span>);
760<span class="hljs-comment">// Prints &#x3C;Buffer 1a>, data truncated when first non-hexadecimal value</span>
761<span class="hljs-comment">// ('g') encountered.</span>
762
763<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'1a7'</span>, <span class="hljs-string">'hex'</span>);
764<span class="hljs-comment">// Prints &#x3C;Buffer 1a>, data truncated when data ends in single digit ('7').</span>
765
766<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'1634'</span>, <span class="hljs-string">'hex'</span>);
767<span class="hljs-comment">// Prints &#x3C;Buffer 16 34>, all data represented.</span></code><button class="copy-button">copy</button></pre>
768<p>Modern Web browsers follow the <a href="https://encoding.spec.whatwg.org/">WHATWG Encoding Standard</a> which aliases
769both <code>'latin1'</code> and <code>'ISO-8859-1'</code> to <code>'win-1252'</code>. This means that while doing
770something like <code>http.get()</code>, if the returned charset is one of those listed in
771the WHATWG specification it is possible that the server actually returned
772<code>'win-1252'</code>-encoded data, and using <code>'latin1'</code> encoding may incorrectly decode
773the characters.</p>
774</section><section><h3>Buffers and TypedArrays<span><a class="mark" href="#buffers-and-typedarrays" id="buffers-and-typedarrays">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffers_and_typedarrays"></a></h3>
775<div class="api_metadata">
776<details class="changelog"><summary>History</summary>
777<table>
778<tbody><tr><th>Version</th><th>Changes</th></tr>
779<tr><td>v3.0.0</td>
780<td><p>The <code>Buffer</code>s class now inherits from <code>Uint8Array</code>.</p></td></tr>
781</tbody></table>
782</details>
783</div>
784<p><code>Buffer</code> instances are also JavaScript <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>
785instances. All <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> methods are available on <code>Buffer</code>s. There are,
786however, subtle incompatibilities between the <code>Buffer</code> API and the
787<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> API.</p>
788<p>In particular:</p>
789<ul>
790<li>While <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/slice"><code>TypedArray.prototype.slice()</code></a> creates a copy of part of the <code>TypedArray</code>,
791<a href="#bufslicestart-end"><code>Buffer.prototype.slice()</code></a> creates a view over the existing <code>Buffer</code>
792without copying. This behavior can be surprising, and only exists for legacy
793compatibility. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray"><code>TypedArray.prototype.subarray()</code></a> can be used to achieve
794the behavior of <a href="#bufslicestart-end"><code>Buffer.prototype.slice()</code></a> on both <code>Buffer</code>s
795and other <code>TypedArray</code>s and should be preferred.</li>
796<li><a href="#buftostringencoding-start-end"><code>buf.toString()</code></a> is incompatible with its <code>TypedArray</code> equivalent.</li>
797<li>A number of methods, e.g. <a href="#bufindexofvalue-byteoffset-encoding"><code>buf.indexOf()</code></a>, support additional arguments.</li>
798</ul>
799<p>There are two ways to create new <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> instances from a <code>Buffer</code>:</p>
800<ul>
801<li>Passing a <code>Buffer</code> to a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> constructor will copy the <code>Buffer</code>s
802contents, interpreted as an array of integers, and not as a byte sequence
803of the target type.</li>
804</ul>
805
806<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
807
808<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]);
809<span class="hljs-keyword">const</span> uint32array = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint32Array</span>(buf);
810
811<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(uint32array);
812
813<span class="hljs-comment">// Prints: Uint32Array(4) [ 1, 2, 3, 4 ]</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
814
815<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]);
816<span class="hljs-keyword">const</span> uint32array = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint32Array</span>(buf);
817
818<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(uint32array);
819
820<span class="hljs-comment">// Prints: Uint32Array(4) [ 1, 2, 3, 4 ]</span></code><button class="copy-button">copy</button></pre>
821<ul>
822<li>Passing the <code>Buffer</code>s underlying <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> will create a
823<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> that shares its memory with the <code>Buffer</code>.</li>
824</ul>
825
826<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
827
828<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'hello'</span>, <span class="hljs-string">'utf16le'</span>);
829<span class="hljs-keyword">const</span> uint16array = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>(
830  buf.<span class="hljs-property">buffer</span>,
831  buf.<span class="hljs-property">byteOffset</span>,
832  buf.<span class="hljs-property">length</span> / <span class="hljs-title class_">Uint16Array</span>.<span class="hljs-property">BYTES_PER_ELEMENT</span>);
833
834<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(uint16array);
835
836<span class="hljs-comment">// Prints: Uint16Array(5) [ 104, 101, 108, 108, 111 ]</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
837
838<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'hello'</span>, <span class="hljs-string">'utf16le'</span>);
839<span class="hljs-keyword">const</span> uint16array = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>(
840  buf.<span class="hljs-property">buffer</span>,
841  buf.<span class="hljs-property">byteOffset</span>,
842  buf.<span class="hljs-property">length</span> / <span class="hljs-title class_">Uint16Array</span>.<span class="hljs-property">BYTES_PER_ELEMENT</span>);
843
844<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(uint16array);
845
846<span class="hljs-comment">// Prints: Uint16Array(5) [ 104, 101, 108, 108, 111 ]</span></code><button class="copy-button">copy</button></pre>
847<p>It is possible to create a new <code>Buffer</code> that shares the same allocated
848memory as a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> instance by using the <code>TypedArray</code> object's
849<code>.buffer</code> property in the same way. <a href="#static-method-bufferfromarraybuffer-byteoffset-length"><code>Buffer.from()</code></a>
850behaves like <code>new Uint8Array()</code> in this context.</p>
851
852<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
853
854<span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>(<span class="hljs-number">2</span>);
855
856arr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>;
857arr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>;
858
859<span class="hljs-comment">// Copies the contents of `arr`.</span>
860<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arr);
861
862<span class="hljs-comment">// Shares memory with `arr`.</span>
863<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arr.<span class="hljs-property">buffer</span>);
864
865<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
866<span class="hljs-comment">// Prints: &#x3C;Buffer 88 a0></span>
867<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2);
868<span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 a0 0f></span>
869
870arr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>;
871
872<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
873<span class="hljs-comment">// Prints: &#x3C;Buffer 88 a0></span>
874<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2);
875<span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 70 17></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
876
877<span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>(<span class="hljs-number">2</span>);
878
879arr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>;
880arr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>;
881
882<span class="hljs-comment">// Copies the contents of `arr`.</span>
883<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arr);
884
885<span class="hljs-comment">// Shares memory with `arr`.</span>
886<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arr.<span class="hljs-property">buffer</span>);
887
888<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
889<span class="hljs-comment">// Prints: &#x3C;Buffer 88 a0></span>
890<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2);
891<span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 a0 0f></span>
892
893arr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>;
894
895<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
896<span class="hljs-comment">// Prints: &#x3C;Buffer 88 a0></span>
897<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2);
898<span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 70 17></span></code><button class="copy-button">copy</button></pre>
899<p>When creating a <code>Buffer</code> using a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>'s <code>.buffer</code>, it is
900possible to use only a portion of the underlying <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> by passing in
901<code>byteOffset</code> and <code>length</code> parameters.</p>
902
903<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
904
905<span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>(<span class="hljs-number">20</span>);
906<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arr.<span class="hljs-property">buffer</span>, <span class="hljs-number">0</span>, <span class="hljs-number">16</span>);
907
908<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>);
909<span class="hljs-comment">// Prints: 16</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
910
911<span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>(<span class="hljs-number">20</span>);
912<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arr.<span class="hljs-property">buffer</span>, <span class="hljs-number">0</span>, <span class="hljs-number">16</span>);
913
914<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>);
915<span class="hljs-comment">// Prints: 16</span></code><button class="copy-button">copy</button></pre>
916<p>The <code>Buffer.from()</code> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from"><code>TypedArray.from()</code></a> have different signatures and
917implementations. Specifically, the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> variants accept a second
918argument that is a mapping function that is invoked on every element of the
919typed array:</p>
920<ul>
921<li><code>TypedArray.from(source[, mapFn[, thisArg]])</code></li>
922</ul>
923<p>The <code>Buffer.from()</code> method, however, does not support the use of a mapping
924function:</p>
925<ul>
926<li><a href="#static-method-bufferfromarray"><code>Buffer.from(array)</code></a></li>
927<li><a href="#static-method-bufferfrombuffer"><code>Buffer.from(buffer)</code></a></li>
928<li><a href="#static-method-bufferfromarraybuffer-byteoffset-length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a></li>
929<li><a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string[, encoding])</code></a></li>
930</ul>
931</section><section><h3>Buffers and iteration<span><a class="mark" href="#buffers-and-iteration" id="buffers-and-iteration">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffers_and_iteration"></a></h3>
932<p><code>Buffer</code> instances can be iterated over using <code>for..of</code> syntax:</p>
933
934<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
935
936<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]);
937
938<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> b <span class="hljs-keyword">of</span> buf) {
939  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b);
940}
941<span class="hljs-comment">// Prints:</span>
942<span class="hljs-comment">//   1</span>
943<span class="hljs-comment">//   2</span>
944<span class="hljs-comment">//   3</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
945
946<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]);
947
948<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> b <span class="hljs-keyword">of</span> buf) {
949  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b);
950}
951<span class="hljs-comment">// Prints:</span>
952<span class="hljs-comment">//   1</span>
953<span class="hljs-comment">//   2</span>
954<span class="hljs-comment">//   3</span></code><button class="copy-button">copy</button></pre>
955<p>Additionally, the <a href="#bufvalues"><code>buf.values()</code></a>, <a href="#bufkeys"><code>buf.keys()</code></a>, and
956<a href="#bufentries"><code>buf.entries()</code></a> methods can be used to create iterators.</p>
957</section><section><h3>Class: <code>Blob</code><span><a class="mark" href="#class-blob" id="class-blob">#</a></span><a aria-hidden="true" class="legacy" id="buffer_class_blob"></a></h3>
958<div class="api_metadata">
959<details class="changelog"><summary>History</summary>
960<table>
961<tbody><tr><th>Version</th><th>Changes</th></tr>
962<tr><td>v18.0.0</td>
963<td><p>No longer experimental.</p></td></tr>
964<tr><td>v15.7.0, v14.18.0</td>
965<td><p><span>Added in: v15.7.0, v14.18.0</span></p></td></tr>
966</tbody></table>
967</details>
968</div>
969<p>A <a href="https://developer.mozilla.org/en-US/docs/Web/API/Blob"><code>Blob</code></a> encapsulates immutable, raw data that can be safely shared across
970multiple worker threads.</p>
971<h4><code>new buffer.Blob([sources[, options]])</code><span><a class="mark" href="#new-bufferblobsources-options" id="new-bufferblobsources-options">#</a></span><a aria-hidden="true" class="legacy" id="buffer_new_buffer_blob_sources_options"></a></h4>
972<div class="api_metadata">
973<details class="changelog"><summary>History</summary>
974<table>
975<tbody><tr><th>Version</th><th>Changes</th></tr>
976<tr><td>v16.7.0</td>
977<td><p>Added the standard <code>endings</code> option to replace line-endings, and removed the non-standard <code>encoding</code> option.</p></td></tr>
978<tr><td>v15.7.0, v14.18.0</td>
979<td><p><span>Added in: v15.7.0, v14.18.0</span></p></td></tr>
980</tbody></table>
981</details>
982</div>
983<ul>
984<li><code>sources</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView[]></a> | <a href="buffer.html#class-blob" class="type">&#x3C;Blob[]></a> An
985array of string, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a>, or <a href="buffer.html#class-blob" class="type">&#x3C;Blob></a> objects,
986or any mix of such objects, that will be stored within the <code>Blob</code>.</li>
987<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
988<ul>
989<li><code>endings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> One of either <code>'transparent'</code> or <code>'native'</code>. When set
990to <code>'native'</code>, line endings in string source parts will be converted to
991the platform native line-ending as specified by <code>require('node:os').EOL</code>.</li>
992<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The Blob content-type. The intent is for <code>type</code> to convey
993the MIME media type of the data, however no validation of the type format
994is performed.</li>
995</ul>
996</li>
997</ul>
998<p>Creates a new <code>Blob</code> object containing a concatenation of the given sources.</p>
999<p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a>, and <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> sources are copied into
1000the 'Blob' and can therefore be safely modified after the 'Blob' is created.</p>
1001<p>String sources are encoded as UTF-8 byte sequences and copied into the Blob.
1002Unmatched surrogate pairs within each string part will be replaced by Unicode
1003U+FFFD replacement characters.</p>
1004<h4><code>blob.arrayBuffer()</code><span><a class="mark" href="#blobarraybuffer" id="blobarraybuffer">#</a></span><a aria-hidden="true" class="legacy" id="buffer_blob_arraybuffer"></a></h4>
1005<div class="api_metadata">
1006<span>Added in: v15.7.0, v14.18.0</span>
1007</div>
1008<ul>
1009<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a></li>
1010</ul>
1011<p>Returns a promise that fulfills with an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a> containing a copy of
1012the <code>Blob</code> data.</p>
1013<h4><code>blob.size</code><span><a class="mark" href="#blobsize" id="blobsize">#</a></span><a aria-hidden="true" class="legacy" id="buffer_blob_size"></a></h4>
1014<div class="api_metadata">
1015<span>Added in: v15.7.0, v14.18.0</span>
1016</div>
1017<p>The total size of the <code>Blob</code> in bytes.</p>
1018<h4><code>blob.slice([start[, end[, type]]])</code><span><a class="mark" href="#blobslicestart-end-type" id="blobslicestart-end-type">#</a></span><a aria-hidden="true" class="legacy" id="buffer_blob_slice_start_end_type"></a></h4>
1019<div class="api_metadata">
1020<span>Added in: v15.7.0, v14.18.0</span>
1021</div>
1022<ul>
1023<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The starting index.</li>
1024<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The ending index.</li>
1025<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The content-type for the new <code>Blob</code></li>
1026</ul>
1027<p>Creates and returns a new <code>Blob</code> containing a subset of this <code>Blob</code> objects
1028data. The original <code>Blob</code> is not altered.</p>
1029<h4><code>blob.stream()</code><span><a class="mark" href="#blobstream" id="blobstream">#</a></span><a aria-hidden="true" class="legacy" id="buffer_blob_stream"></a></h4>
1030<div class="api_metadata">
1031<span>Added in: v16.7.0</span>
1032</div>
1033<ul>
1034<li>Returns: <a href="webstreams.html#class-readablestream" class="type">&#x3C;ReadableStream></a></li>
1035</ul>
1036<p>Returns a new <code>ReadableStream</code> that allows the content of the <code>Blob</code> to be read.</p>
1037<h4><code>blob.text()</code><span><a class="mark" href="#blobtext" id="blobtext">#</a></span><a aria-hidden="true" class="legacy" id="buffer_blob_text"></a></h4>
1038<div class="api_metadata">
1039<span>Added in: v15.7.0, v14.18.0</span>
1040</div>
1041<ul>
1042<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a></li>
1043</ul>
1044<p>Returns a promise that fulfills with the contents of the <code>Blob</code> decoded as a
1045UTF-8 string.</p>
1046<h4><code>blob.type</code><span><a class="mark" href="#blobtype" id="blobtype">#</a></span><a aria-hidden="true" class="legacy" id="buffer_blob_type"></a></h4>
1047<div class="api_metadata">
1048<span>Added in: v15.7.0, v14.18.0</span>
1049</div>
1050<ul>
1051<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
1052</ul>
1053<p>The content-type of the <code>Blob</code>.</p>
1054<h4><code>Blob</code> objects and <code>MessageChannel</code><span><a class="mark" href="#blob-objects-and-messagechannel" id="blob-objects-and-messagechannel">#</a></span><a aria-hidden="true" class="legacy" id="buffer_blob_objects_and_messagechannel"></a></h4>
1055<p>Once a <a href="buffer.html#class-blob" class="type">&#x3C;Blob></a> object is created, it can be sent via <code>MessagePort</code> to multiple
1056destinations without transferring or immediately copying the data. The data
1057contained by the <code>Blob</code> is copied only when the <code>arrayBuffer()</code> or <code>text()</code>
1058methods are called.</p>
1059
1060<pre class="with-62-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Blob</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1061<span class="hljs-keyword">import</span> { <span class="hljs-built_in">setTimeout</span> <span class="hljs-keyword">as</span> delay } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:timers/promises'</span>;
1062
1063<span class="hljs-keyword">const</span> blob = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Blob</span>([<span class="hljs-string">'hello there'</span>]);
1064
1065<span class="hljs-keyword">const</span> mc1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>();
1066<span class="hljs-keyword">const</span> mc2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>();
1067
1068mc1.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">async</span> ({ data }) => {
1069  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">await</span> data.<span class="hljs-title function_">arrayBuffer</span>());
1070  mc1.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>();
1071};
1072
1073mc2.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">async</span> ({ data }) => {
1074  <span class="hljs-keyword">await</span> <span class="hljs-title function_">delay</span>(<span class="hljs-number">1000</span>);
1075  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">await</span> data.<span class="hljs-title function_">arrayBuffer</span>());
1076  mc2.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>();
1077};
1078
1079mc1.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(blob);
1080mc2.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(blob);
1081
1082<span class="hljs-comment">// The Blob is still usable after posting.</span>
1083blob.<span class="hljs-title function_">text</span>().<span class="hljs-title function_">then</span>(<span class="hljs-variable language_">console</span>.<span class="hljs-property">log</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Blob</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1084<span class="hljs-keyword">const</span> { <span class="hljs-attr">setTimeout</span>: delay } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:timers/promises'</span>);
1085
1086<span class="hljs-keyword">const</span> blob = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Blob</span>([<span class="hljs-string">'hello there'</span>]);
1087
1088<span class="hljs-keyword">const</span> mc1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>();
1089<span class="hljs-keyword">const</span> mc2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>();
1090
1091mc1.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">async</span> ({ data }) => {
1092  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">await</span> data.<span class="hljs-title function_">arrayBuffer</span>());
1093  mc1.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>();
1094};
1095
1096mc2.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">async</span> ({ data }) => {
1097  <span class="hljs-keyword">await</span> <span class="hljs-title function_">delay</span>(<span class="hljs-number">1000</span>);
1098  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-keyword">await</span> data.<span class="hljs-title function_">arrayBuffer</span>());
1099  mc2.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>();
1100};
1101
1102mc1.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(blob);
1103mc2.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(blob);
1104
1105<span class="hljs-comment">// The Blob is still usable after posting.</span>
1106blob.<span class="hljs-title function_">text</span>().<span class="hljs-title function_">then</span>(<span class="hljs-variable language_">console</span>.<span class="hljs-property">log</span>);</code><button class="copy-button">copy</button></pre>
1107</section><section><h3>Class: <code>Buffer</code><span><a class="mark" href="#class-buffer" id="class-buffer">#</a></span><a aria-hidden="true" class="legacy" id="buffer_class_buffer"></a></h3>
1108<p>The <code>Buffer</code> class is a global type for dealing with binary data directly.
1109It can be constructed in a variety of ways.</p>
1110<h4>Static method: <code>Buffer.alloc(size[, fill[, encoding]])</code><span><a class="mark" href="#static-method-bufferallocsize-fill-encoding" id="static-method-bufferallocsize-fill-encoding">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_alloc_size_fill_encoding"></a></h4>
1111<div class="api_metadata">
1112<details class="changelog"><summary>History</summary>
1113<table>
1114<tbody><tr><th>Version</th><th>Changes</th></tr>
1115<tr><td>v20.0.0</td>
1116<td><p>Throw ERR_INVALID_ARG_TYPE or ERR_OUT_OF_RANGE instead of ERR_INVALID_ARG_VALUE for invalid input arguments.</p></td></tr>
1117<tr><td>v15.0.0</td>
1118<td><p>Throw ERR_INVALID_ARG_VALUE instead of ERR_INVALID_OPT_VALUE for invalid input arguments.</p></td></tr>
1119<tr><td>v10.0.0</td>
1120<td><p>Attempting to fill a non-zero length buffer with a zero length buffer triggers a thrown exception.</p></td></tr>
1121<tr><td>v10.0.0</td>
1122<td><p>Specifying an invalid string for <code>fill</code> triggers a thrown exception.</p></td></tr>
1123<tr><td>v8.9.3</td>
1124<td><p>Specifying an invalid string for <code>fill</code> now results in a zero-filled buffer.</p></td></tr>
1125<tr><td>v5.10.0</td>
1126<td><p><span>Added in: v5.10.0</span></p></td></tr>
1127</tbody></table>
1128</details>
1129</div>
1130<ul>
1131<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The desired length of the new <code>Buffer</code>.</li>
1132<li><code>fill</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> A value to pre-fill the new <code>Buffer</code>
1133with. <strong>Default:</strong> <code>0</code>.</li>
1134<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> If <code>fill</code> is a string, this is its encoding.
1135<strong>Default:</strong> <code>'utf8'</code>.</li>
1136</ul>
1137<p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>fill</code> is <code>undefined</code>, the
1138<code>Buffer</code> will be zero-filled.</p>
1139
1140<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1141
1142<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">5</span>);
1143
1144<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
1145<span class="hljs-comment">// Prints: &#x3C;Buffer 00 00 00 00 00></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1146
1147<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">5</span>);
1148
1149<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
1150<span class="hljs-comment">// Prints: &#x3C;Buffer 00 00 00 00 00></span></code><button class="copy-button">copy</button></pre>
1151<p>If <code>size</code> is larger than
1152<a href="#bufferconstantsmax_length"><code>buffer.constants.MAX_LENGTH</code></a> or smaller than 0, <a href="errors.html#err_out_of_range"><code>ERR_OUT_OF_RANGE</code></a>
1153is thrown.</p>
1154<p>If <code>fill</code> is specified, the allocated <code>Buffer</code> will be initialized by calling
1155<a href="#buffillvalue-offset-end-encoding"><code>buf.fill(fill)</code></a>.</p>
1156
1157<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1158
1159<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">5</span>, <span class="hljs-string">'a'</span>);
1160
1161<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
1162<span class="hljs-comment">// Prints: &#x3C;Buffer 61 61 61 61 61></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1163
1164<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">5</span>, <span class="hljs-string">'a'</span>);
1165
1166<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
1167<span class="hljs-comment">// Prints: &#x3C;Buffer 61 61 61 61 61></span></code><button class="copy-button">copy</button></pre>
1168<p>If both <code>fill</code> and <code>encoding</code> are specified, the allocated <code>Buffer</code> will be
1169initialized by calling <a href="#buffillvalue-offset-end-encoding"><code>buf.fill(fill, encoding)</code></a>.</p>
1170
1171<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1172
1173<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">11</span>, <span class="hljs-string">'aGVsbG8gd29ybGQ='</span>, <span class="hljs-string">'base64'</span>);
1174
1175<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
1176<span class="hljs-comment">// Prints: &#x3C;Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1177
1178<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">11</span>, <span class="hljs-string">'aGVsbG8gd29ybGQ='</span>, <span class="hljs-string">'base64'</span>);
1179
1180<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
1181<span class="hljs-comment">// Prints: &#x3C;Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64></span></code><button class="copy-button">copy</button></pre>
1182<p>Calling <a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc()</code></a> can be measurably slower than the alternative
1183<a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> but ensures that the newly created <code>Buffer</code> instance
1184contents will never contain sensitive data from previous allocations, including
1185data that might not have been allocated for <code>Buffer</code>s.</p>
1186<p>A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</p>
1187<h4>Static method: <code>Buffer.allocUnsafe(size)</code><span><a class="mark" href="#static-method-bufferallocunsafesize" id="static-method-bufferallocunsafesize">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_allocunsafe_size"></a></h4>
1188<div class="api_metadata">
1189<details class="changelog"><summary>History</summary>
1190<table>
1191<tbody><tr><th>Version</th><th>Changes</th></tr>
1192<tr><td>v20.0.0</td>
1193<td><p>Throw ERR_INVALID_ARG_TYPE or ERR_OUT_OF_RANGE instead of ERR_INVALID_ARG_VALUE for invalid input arguments.</p></td></tr>
1194<tr><td>v15.0.0</td>
1195<td><p>Throw ERR_INVALID_ARG_VALUE instead of ERR_INVALID_OPT_VALUE for invalid input arguments.</p></td></tr>
1196<tr><td>v7.0.0</td>
1197<td><p>Passing a negative <code>size</code> will now throw an error.</p></td></tr>
1198<tr><td>v5.10.0</td>
1199<td><p><span>Added in: v5.10.0</span></p></td></tr>
1200</tbody></table>
1201</details>
1202</div>
1203<ul>
1204<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The desired length of the new <code>Buffer</code>.</li>
1205</ul>
1206<p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>size</code> is larger than
1207<a href="#bufferconstantsmax_length"><code>buffer.constants.MAX_LENGTH</code></a> or smaller than 0, <a href="errors.html#err_out_of_range"><code>ERR_OUT_OF_RANGE</code></a>
1208is thrown.</p>
1209<p>The underlying memory for <code>Buffer</code> instances created in this way is <em>not
1210initialized</em>. The contents of the newly created <code>Buffer</code> are unknown and
1211<em>may contain sensitive data</em>. Use <a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc()</code></a> instead to initialize
1212<code>Buffer</code> instances with zeroes.</p>
1213
1214<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1215
1216<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">10</span>);
1217
1218<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
1219<span class="hljs-comment">// Prints (contents may vary): &#x3C;Buffer a0 8b 28 3f 01 00 00 00 50 32></span>
1220
1221buf.<span class="hljs-title function_">fill</span>(<span class="hljs-number">0</span>);
1222
1223<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
1224<span class="hljs-comment">// Prints: &#x3C;Buffer 00 00 00 00 00 00 00 00 00 00></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1225
1226<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">10</span>);
1227
1228<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
1229<span class="hljs-comment">// Prints (contents may vary): &#x3C;Buffer a0 8b 28 3f 01 00 00 00 50 32></span>
1230
1231buf.<span class="hljs-title function_">fill</span>(<span class="hljs-number">0</span>);
1232
1233<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
1234<span class="hljs-comment">// Prints: &#x3C;Buffer 00 00 00 00 00 00 00 00 00 00></span></code><button class="copy-button">copy</button></pre>
1235<p>A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</p>
1236<p>The <code>Buffer</code> module pre-allocates an internal <code>Buffer</code> instance of
1237size <a href="#class-property-bufferpoolsize"><code>Buffer.poolSize</code></a> that is used as a pool for the fast allocation of new
1238<code>Buffer</code> instances created using <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a>, <a href="#static-method-bufferfromarray"><code>Buffer.from(array)</code></a>,
1239and <a href="#static-method-bufferconcatlist-totallength"><code>Buffer.concat()</code></a> only when <code>size</code> is less than or equal to
1240<code>Buffer.poolSize >> 1</code> (floor of <a href="#class-property-bufferpoolsize"><code>Buffer.poolSize</code></a> divided by two).</p>
1241<p>Use of this pre-allocated internal memory pool is a key difference between
1242calling <code>Buffer.alloc(size, fill)</code> vs. <code>Buffer.allocUnsafe(size).fill(fill)</code>.
1243Specifically, <code>Buffer.alloc(size, fill)</code> will <em>never</em> use the internal <code>Buffer</code>
1244pool, while <code>Buffer.allocUnsafe(size).fill(fill)</code> <em>will</em> use the internal
1245<code>Buffer</code> pool if <code>size</code> is less than or equal to half <a href="#class-property-bufferpoolsize"><code>Buffer.poolSize</code></a>. The
1246difference is subtle but can be important when an application requires the
1247additional performance that <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> provides.</p>
1248<h4>Static method: <code>Buffer.allocUnsafeSlow(size)</code><span><a class="mark" href="#static-method-bufferallocunsafeslowsize" id="static-method-bufferallocunsafeslowsize">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_allocunsafeslow_size"></a></h4>
1249<div class="api_metadata">
1250<details class="changelog"><summary>History</summary>
1251<table>
1252<tbody><tr><th>Version</th><th>Changes</th></tr>
1253<tr><td>v20.0.0</td>
1254<td><p>Throw ERR_INVALID_ARG_TYPE or ERR_OUT_OF_RANGE instead of ERR_INVALID_ARG_VALUE for invalid input arguments.</p></td></tr>
1255<tr><td>v15.0.0</td>
1256<td><p>Throw ERR_INVALID_ARG_VALUE instead of ERR_INVALID_OPT_VALUE for invalid input arguments.</p></td></tr>
1257<tr><td>v5.12.0</td>
1258<td><p><span>Added in: v5.12.0</span></p></td></tr>
1259</tbody></table>
1260</details>
1261</div>
1262<ul>
1263<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The desired length of the new <code>Buffer</code>.</li>
1264</ul>
1265<p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>size</code> is larger than
1266<a href="#bufferconstantsmax_length"><code>buffer.constants.MAX_LENGTH</code></a> or smaller than 0, <a href="errors.html#err_out_of_range"><code>ERR_OUT_OF_RANGE</code></a>
1267is thrown. A zero-length <code>Buffer</code> is created if <code>size</code> is 0.</p>
1268<p>The underlying memory for <code>Buffer</code> instances created in this way is <em>not
1269initialized</em>. The contents of the newly created <code>Buffer</code> are unknown and
1270<em>may contain sensitive data</em>. Use <a href="#buffillvalue-offset-end-encoding"><code>buf.fill(0)</code></a> to initialize
1271such <code>Buffer</code> instances with zeroes.</p>
1272<p>When using <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> to allocate new <code>Buffer</code> instances,
1273allocations under 4 KiB are sliced from a single pre-allocated <code>Buffer</code>. This
1274allows applications to avoid the garbage collection overhead of creating many
1275individually allocated <code>Buffer</code> instances. This approach improves both
1276performance and memory usage by eliminating the need to track and clean up as
1277many individual <code>ArrayBuffer</code> objects.</p>
1278<p>However, in the case where a developer may need to retain a small chunk of
1279memory from a pool for an indeterminate amount of time, it may be appropriate
1280to create an un-pooled <code>Buffer</code> instance using <code>Buffer.allocUnsafeSlow()</code> and
1281then copying out the relevant bits.</p>
1282
1283<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1284
1285<span class="hljs-comment">// Need to keep around a few small chunks of memory.</span>
1286<span class="hljs-keyword">const</span> store = [];
1287
1288socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> {
1289  <span class="hljs-keyword">let</span> data;
1290  <span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (data = readable.<span class="hljs-title function_">read</span>())) {
1291    <span class="hljs-comment">// Allocate for retained data.</span>
1292    <span class="hljs-keyword">const</span> sb = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafeSlow</span>(<span class="hljs-number">10</span>);
1293
1294    <span class="hljs-comment">// Copy the data into the new allocation.</span>
1295    data.<span class="hljs-title function_">copy</span>(sb, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">10</span>);
1296
1297    store.<span class="hljs-title function_">push</span>(sb);
1298  }
1299});</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1300
1301<span class="hljs-comment">// Need to keep around a few small chunks of memory.</span>
1302<span class="hljs-keyword">const</span> store = [];
1303
1304socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> {
1305  <span class="hljs-keyword">let</span> data;
1306  <span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (data = readable.<span class="hljs-title function_">read</span>())) {
1307    <span class="hljs-comment">// Allocate for retained data.</span>
1308    <span class="hljs-keyword">const</span> sb = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafeSlow</span>(<span class="hljs-number">10</span>);
1309
1310    <span class="hljs-comment">// Copy the data into the new allocation.</span>
1311    data.<span class="hljs-title function_">copy</span>(sb, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">10</span>);
1312
1313    store.<span class="hljs-title function_">push</span>(sb);
1314  }
1315});</code><button class="copy-button">copy</button></pre>
1316<p>A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</p>
1317<h4>Static method: <code>Buffer.byteLength(string[, encoding])</code><span><a class="mark" href="#static-method-bufferbytelengthstring-encoding" id="static-method-bufferbytelengthstring-encoding">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_bytelength_string_encoding"></a></h4>
1318<div class="api_metadata">
1319<details class="changelog"><summary>History</summary>
1320<table>
1321<tbody><tr><th>Version</th><th>Changes</th></tr>
1322<tr><td>v7.0.0</td>
1323<td><p>Passing invalid input will now throw an error.</p></td></tr>
1324<tr><td>v5.10.0</td>
1325<td><p>The <code>string</code> parameter can now be any <code>TypedArray</code>, <code>DataView</code> or <code>ArrayBuffer</code>.</p></td></tr>
1326<tr><td>v0.1.90</td>
1327<td><p><span>Added in: v0.1.90</span></p></td></tr>
1328</tbody></table>
1329</details>
1330</div>
1331<ul>
1332<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer" class="type">&#x3C;SharedArrayBuffer></a> A
1333value to calculate the length of.</li>
1334<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> If <code>string</code> is a string, this is its encoding.
1335<strong>Default:</strong> <code>'utf8'</code>.</li>
1336<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The number of bytes contained within <code>string</code>.</li>
1337</ul>
1338<p>Returns the byte length of a string when encoded using <code>encoding</code>.
1339This is not the same as <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length"><code>String.prototype.length</code></a>, which does not account
1340for the encoding that is used to convert the string into bytes.</p>
1341<p>For <code>'base64'</code>, <code>'base64url'</code>, and <code>'hex'</code>, this function assumes valid input.
1342For strings that contain non-base64/hex-encoded data (e.g. whitespace), the
1343return value might be greater than the length of a <code>Buffer</code> created from the
1344string.</p>
1345
1346<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1347
1348<span class="hljs-keyword">const</span> str = <span class="hljs-string">'\u00bd + \u00bc = \u00be'</span>;
1349
1350<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${str}</span>: <span class="hljs-subst">${str.length}</span> characters, `</span> +
1351            <span class="hljs-string">`<span class="hljs-subst">${Buffer.byteLength(str, <span class="hljs-string">'utf8'</span>)}</span> bytes`</span>);
1352<span class="hljs-comment">// Prints: ½ + ¼ = ¾: 9 characters, 12 bytes</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1353
1354<span class="hljs-keyword">const</span> str = <span class="hljs-string">'\u00bd + \u00bc = \u00be'</span>;
1355
1356<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${str}</span>: <span class="hljs-subst">${str.length}</span> characters, `</span> +
1357            <span class="hljs-string">`<span class="hljs-subst">${Buffer.byteLength(str, <span class="hljs-string">'utf8'</span>)}</span> bytes`</span>);
1358<span class="hljs-comment">// Prints: ½ + ¼ = ¾: 9 characters, 12 bytes</span></code><button class="copy-button">copy</button></pre>
1359<p>When <code>string</code> is a <code>Buffer</code>/<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView"><code>DataView</code></a>/<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>/<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a>/
1360<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a>, the byte length as reported by <code>.byteLength</code>
1361is returned.</p>
1362<h4>Static method: <code>Buffer.compare(buf1, buf2)</code><span><a class="mark" href="#static-method-buffercomparebuf1-buf2" id="static-method-buffercomparebuf1-buf2">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_compare_buf1_buf2"></a></h4>
1363<div class="api_metadata">
1364<details class="changelog"><summary>History</summary>
1365<table>
1366<tbody><tr><th>Version</th><th>Changes</th></tr>
1367<tr><td>v8.0.0</td>
1368<td><p>The arguments can now be <code>Uint8Array</code>s.</p></td></tr>
1369<tr><td>v0.11.13</td>
1370<td><p><span>Added in: v0.11.13</span></p></td></tr>
1371</tbody></table>
1372</details>
1373</div>
1374<ul>
1375<li><code>buf1</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a></li>
1376<li><code>buf2</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a></li>
1377<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Either <code>-1</code>, <code>0</code>, or <code>1</code>, depending on the result of the
1378comparison. See <a href="#bufcomparetarget-targetstart-targetend-sourcestart-sourceend"><code>buf.compare()</code></a> for details.</li>
1379</ul>
1380<p>Compares <code>buf1</code> to <code>buf2</code>, typically for the purpose of sorting arrays of
1381<code>Buffer</code> instances. This is equivalent to calling
1382<a href="#bufcomparetarget-targetstart-targetend-sourcestart-sourceend"><code>buf1.compare(buf2)</code></a>.</p>
1383
1384<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1385
1386<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'1234'</span>);
1387<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'0123'</span>);
1388<span class="hljs-keyword">const</span> arr = [buf1, buf2];
1389
1390<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(arr.<span class="hljs-title function_">sort</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-property">compare</span>));
1391<span class="hljs-comment">// Prints: [ &#x3C;Buffer 30 31 32 33>, &#x3C;Buffer 31 32 33 34> ]</span>
1392<span class="hljs-comment">// (This result is equal to: [buf2, buf1].)</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1393
1394<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'1234'</span>);
1395<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'0123'</span>);
1396<span class="hljs-keyword">const</span> arr = [buf1, buf2];
1397
1398<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(arr.<span class="hljs-title function_">sort</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-property">compare</span>));
1399<span class="hljs-comment">// Prints: [ &#x3C;Buffer 30 31 32 33>, &#x3C;Buffer 31 32 33 34> ]</span>
1400<span class="hljs-comment">// (This result is equal to: [buf2, buf1].)</span></code><button class="copy-button">copy</button></pre>
1401<h4>Static method: <code>Buffer.concat(list[, totalLength])</code><span><a class="mark" href="#static-method-bufferconcatlist-totallength" id="static-method-bufferconcatlist-totallength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_concat_list_totallength"></a></h4>
1402<div class="api_metadata">
1403<details class="changelog"><summary>History</summary>
1404<table>
1405<tbody><tr><th>Version</th><th>Changes</th></tr>
1406<tr><td>v8.0.0</td>
1407<td><p>The elements of <code>list</code> can now be <code>Uint8Array</code>s.</p></td></tr>
1408<tr><td>v0.7.11</td>
1409<td><p><span>Added in: v0.7.11</span></p></td></tr>
1410</tbody></table>
1411</details>
1412</div>
1413<ul>
1414<li><code>list</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array[]></a> List of <code>Buffer</code> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a>
1415instances to concatenate.</li>
1416<li><code>totalLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Total length of the <code>Buffer</code> instances in <code>list</code>
1417when concatenated.</li>
1418<li>Returns: <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
1419</ul>
1420<p>Returns a new <code>Buffer</code> which is the result of concatenating all the <code>Buffer</code>
1421instances in the <code>list</code> together.</p>
1422<p>If the list has no items, or if the <code>totalLength</code> is 0, then a new zero-length
1423<code>Buffer</code> is returned.</p>
1424<p>If <code>totalLength</code> is not provided, it is calculated from the <code>Buffer</code> instances
1425in <code>list</code> by adding their lengths.</p>
1426<p>If <code>totalLength</code> is provided, it is coerced to an unsigned integer. If the
1427combined length of the <code>Buffer</code>s in <code>list</code> exceeds <code>totalLength</code>, the result is
1428truncated to <code>totalLength</code>.</p>
1429
1430<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1431
1432<span class="hljs-comment">// Create a single `Buffer` from a list of three `Buffer` instances.</span>
1433
1434<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">10</span>);
1435<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">14</span>);
1436<span class="hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">18</span>);
1437<span class="hljs-keyword">const</span> totalLength = buf1.<span class="hljs-property">length</span> + buf2.<span class="hljs-property">length</span> + buf3.<span class="hljs-property">length</span>;
1438
1439<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(totalLength);
1440<span class="hljs-comment">// Prints: 42</span>
1441
1442<span class="hljs-keyword">const</span> bufA = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">concat</span>([buf1, buf2, buf3], totalLength);
1443
1444<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(bufA);
1445<span class="hljs-comment">// Prints: &#x3C;Buffer 00 00 00 00 ...></span>
1446<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(bufA.<span class="hljs-property">length</span>);
1447<span class="hljs-comment">// Prints: 42</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1448
1449<span class="hljs-comment">// Create a single `Buffer` from a list of three `Buffer` instances.</span>
1450
1451<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">10</span>);
1452<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">14</span>);
1453<span class="hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">18</span>);
1454<span class="hljs-keyword">const</span> totalLength = buf1.<span class="hljs-property">length</span> + buf2.<span class="hljs-property">length</span> + buf3.<span class="hljs-property">length</span>;
1455
1456<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(totalLength);
1457<span class="hljs-comment">// Prints: 42</span>
1458
1459<span class="hljs-keyword">const</span> bufA = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">concat</span>([buf1, buf2, buf3], totalLength);
1460
1461<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(bufA);
1462<span class="hljs-comment">// Prints: &#x3C;Buffer 00 00 00 00 ...></span>
1463<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(bufA.<span class="hljs-property">length</span>);
1464<span class="hljs-comment">// Prints: 42</span></code><button class="copy-button">copy</button></pre>
1465<p><code>Buffer.concat()</code> may also use the internal <code>Buffer</code> pool like
1466<a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> does.</p>
1467<h4>Static method: <code>Buffer.copyBytesFrom(view[, offset[, length]])</code><span><a class="mark" href="#static-method-buffercopybytesfromview-offset-length" id="static-method-buffercopybytesfromview-offset-length">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_copybytesfrom_view_offset_length"></a></h4>
1468<div class="api_metadata">
1469<span>Added in: v18.16.0</span>
1470</div>
1471<ul>
1472<li><code>view</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> The <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> to copy.</li>
1473<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The starting offset within <code>view</code>. <strong>Default:</strong>: <code>0</code>.</li>
1474<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The number of elements from <code>view</code> to copy.
1475<strong>Default:</strong> <code>view.length - offset</code>.</li>
1476</ul>
1477<p>Copies the underlying memory of <code>view</code> into a new <code>Buffer</code>.</p>
1478<pre><code class="language-js"><span class="hljs-keyword">const</span> u16 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>([<span class="hljs-number">0</span>, <span class="hljs-number">0xffff</span>]);
1479<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">copyBytesFrom</span>(u16, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>);
1480u16[<span class="hljs-number">1</span>] = <span class="hljs-number">0</span>;
1481<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>); <span class="hljs-comment">// 2</span>
1482<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf[<span class="hljs-number">0</span>]); <span class="hljs-comment">// 255</span>
1483<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf[<span class="hljs-number">1</span>]); <span class="hljs-comment">// 255</span></code> <button class="copy-button">copy</button></pre>
1484<h4>Static method: <code>Buffer.from(array)</code><span><a class="mark" href="#static-method-bufferfromarray" id="static-method-bufferfromarray">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_from_array"></a></h4>
1485<div class="api_metadata">
1486<span>Added in: v5.10.0</span>
1487</div>
1488<ul>
1489<li><code>array</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer[]></a></li>
1490</ul>
1491<p>Allocates a new <code>Buffer</code> using an <code>array</code> of bytes in the range <code>0</code> – <code>255</code>.
1492Array entries outside that range will be truncated to fit into it.</p>
1493
1494<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1495
1496<span class="hljs-comment">// Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'.</span>
1497<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x62</span>, <span class="hljs-number">0x75</span>, <span class="hljs-number">0x66</span>, <span class="hljs-number">0x66</span>, <span class="hljs-number">0x65</span>, <span class="hljs-number">0x72</span>]);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1498
1499<span class="hljs-comment">// Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'.</span>
1500<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x62</span>, <span class="hljs-number">0x75</span>, <span class="hljs-number">0x66</span>, <span class="hljs-number">0x66</span>, <span class="hljs-number">0x65</span>, <span class="hljs-number">0x72</span>]);</code><button class="copy-button">copy</button></pre>
1501<p>If <code>array</code> is an <code>Array</code>-like object (that is, one with a <code>length</code> property of
1502type <code>number</code>), it is treated as if it is an array, unless it is a <code>Buffer</code> or
1503a <code>Uint8Array</code>. This means all other <code>TypedArray</code> variants get treated as an
1504<code>Array</code>. To create a <code>Buffer</code> from the bytes backing a <code>TypedArray</code>, use
1505<a href="#static-method-buffercopybytesfromview-offset-length"><code>Buffer.copyBytesFrom()</code></a>.</p>
1506<p>A <code>TypeError</code> will be thrown if <code>array</code> is not an <code>Array</code> or another type
1507appropriate for <code>Buffer.from()</code> variants.</p>
1508<p><code>Buffer.from(array)</code> and <a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string)</code></a> may also use the internal
1509<code>Buffer</code> pool like <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> does.</p>
1510<h4>Static method: <code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code><span><a class="mark" href="#static-method-bufferfromarraybuffer-byteoffset-length" id="static-method-bufferfromarraybuffer-byteoffset-length">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_from_arraybuffer_byteoffset_length"></a></h4>
1511<div class="api_metadata">
1512<span>Added in: v5.10.0</span>
1513</div>
1514<ul>
1515<li><code>arrayBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer" class="type">&#x3C;SharedArrayBuffer></a> An <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a>,
1516<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a>, for example the <code>.buffer</code> property of a
1517<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>.</li>
1518<li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Index of first byte to expose. <strong>Default:</strong> <code>0</code>.</li>
1519<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to expose.
1520<strong>Default:</strong> <code>arrayBuffer.byteLength - byteOffset</code>.</li>
1521</ul>
1522<p>This creates a view of the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> without copying the underlying
1523memory. For example, when passed a reference to the <code>.buffer</code> property of a
1524<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> instance, the newly created <code>Buffer</code> will share the same
1525allocated memory as the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>'s underlying <code>ArrayBuffer</code>.</p>
1526
1527<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1528
1529<span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>(<span class="hljs-number">2</span>);
1530
1531arr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>;
1532arr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>;
1533
1534<span class="hljs-comment">// Shares memory with `arr`.</span>
1535<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arr.<span class="hljs-property">buffer</span>);
1536
1537<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
1538<span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 a0 0f></span>
1539
1540<span class="hljs-comment">// Changing the original Uint16Array changes the Buffer also.</span>
1541arr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>;
1542
1543<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
1544<span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 70 17></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1545
1546<span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint16Array</span>(<span class="hljs-number">2</span>);
1547
1548arr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>;
1549arr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>;
1550
1551<span class="hljs-comment">// Shares memory with `arr`.</span>
1552<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arr.<span class="hljs-property">buffer</span>);
1553
1554<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
1555<span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 a0 0f></span>
1556
1557<span class="hljs-comment">// Changing the original Uint16Array changes the Buffer also.</span>
1558arr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>;
1559
1560<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
1561<span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 70 17></span></code><button class="copy-button">copy</button></pre>
1562<p>The optional <code>byteOffset</code> and <code>length</code> arguments specify a memory range within
1563the <code>arrayBuffer</code> that will be shared by the <code>Buffer</code>.</p>
1564
1565<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1566
1567<span class="hljs-keyword">const</span> ab = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>(<span class="hljs-number">10</span>);
1568<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(ab, <span class="hljs-number">0</span>, <span class="hljs-number">2</span>);
1569
1570<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>);
1571<span class="hljs-comment">// Prints: 2</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1572
1573<span class="hljs-keyword">const</span> ab = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>(<span class="hljs-number">10</span>);
1574<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(ab, <span class="hljs-number">0</span>, <span class="hljs-number">2</span>);
1575
1576<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>);
1577<span class="hljs-comment">// Prints: 2</span></code><button class="copy-button">copy</button></pre>
1578<p>A <code>TypeError</code> will be thrown if <code>arrayBuffer</code> is not an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> or a
1579<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a> or another type appropriate for <code>Buffer.from()</code>
1580variants.</p>
1581<p>It is important to remember that a backing <code>ArrayBuffer</code> can cover a range
1582of memory that extends beyond the bounds of a <code>TypedArray</code> view. A new
1583<code>Buffer</code> created using the <code>buffer</code> property of a <code>TypedArray</code> may extend
1584beyond the range of the <code>TypedArray</code>:</p>
1585
1586<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1587
1588<span class="hljs-keyword">const</span> arrA = <span class="hljs-title class_">Uint8Array</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x63</span>, <span class="hljs-number">0x64</span>, <span class="hljs-number">0x65</span>, <span class="hljs-number">0x66</span>]); <span class="hljs-comment">// 4 elements</span>
1589<span class="hljs-keyword">const</span> arrB = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(arrA.<span class="hljs-property">buffer</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>); <span class="hljs-comment">// 2 elements</span>
1590<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(arrA.<span class="hljs-property">buffer</span> === arrB.<span class="hljs-property">buffer</span>); <span class="hljs-comment">// true</span>
1591
1592<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arrB.<span class="hljs-property">buffer</span>);
1593<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
1594<span class="hljs-comment">// Prints: &#x3C;Buffer 63 64 65 66></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1595
1596<span class="hljs-keyword">const</span> arrA = <span class="hljs-title class_">Uint8Array</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x63</span>, <span class="hljs-number">0x64</span>, <span class="hljs-number">0x65</span>, <span class="hljs-number">0x66</span>]); <span class="hljs-comment">// 4 elements</span>
1597<span class="hljs-keyword">const</span> arrB = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(arrA.<span class="hljs-property">buffer</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>); <span class="hljs-comment">// 2 elements</span>
1598<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(arrA.<span class="hljs-property">buffer</span> === arrB.<span class="hljs-property">buffer</span>); <span class="hljs-comment">// true</span>
1599
1600<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arrB.<span class="hljs-property">buffer</span>);
1601<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
1602<span class="hljs-comment">// Prints: &#x3C;Buffer 63 64 65 66></span></code><button class="copy-button">copy</button></pre>
1603<h4>Static method: <code>Buffer.from(buffer)</code><span><a class="mark" href="#static-method-bufferfrombuffer" id="static-method-bufferfrombuffer">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_from_buffer"></a></h4>
1604<div class="api_metadata">
1605<span>Added in: v5.10.0</span>
1606</div>
1607<ul>
1608<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> An existing <code>Buffer</code> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> from
1609which to copy data.</li>
1610</ul>
1611<p>Copies the passed <code>buffer</code> data onto a new <code>Buffer</code> instance.</p>
1612
1613<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1614
1615<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>);
1616<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(buf1);
1617
1618buf1[<span class="hljs-number">0</span>] = <span class="hljs-number">0x61</span>;
1619
1620<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>());
1621<span class="hljs-comment">// Prints: auffer</span>
1622<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>());
1623<span class="hljs-comment">// Prints: buffer</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1624
1625<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>);
1626<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(buf1);
1627
1628buf1[<span class="hljs-number">0</span>] = <span class="hljs-number">0x61</span>;
1629
1630<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>());
1631<span class="hljs-comment">// Prints: auffer</span>
1632<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>());
1633<span class="hljs-comment">// Prints: buffer</span></code><button class="copy-button">copy</button></pre>
1634<p>A <code>TypeError</code> will be thrown if <code>buffer</code> is not a <code>Buffer</code> or another type
1635appropriate for <code>Buffer.from()</code> variants.</p>
1636<h4>Static method: <code>Buffer.from(object[, offsetOrEncoding[, length]])</code><span><a class="mark" href="#static-method-bufferfromobject-offsetorencoding-length" id="static-method-bufferfromobject-offsetorencoding-length">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_from_object_offsetorencoding_length"></a></h4>
1637<div class="api_metadata">
1638<span>Added in: v8.2.0</span>
1639</div>
1640<ul>
1641<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> An object supporting <code>Symbol.toPrimitive</code> or <code>valueOf()</code>.</li>
1642<li><code>offsetOrEncoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> A byte-offset or encoding.</li>
1643<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> A length.</li>
1644</ul>
1645<p>For objects whose <code>valueOf()</code> function returns a value not strictly equal to
1646<code>object</code>, returns <code>Buffer.from(object.valueOf(), offsetOrEncoding, length)</code>.</p>
1647
1648<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1649
1650<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>(<span class="hljs-string">'this is a test'</span>));
1651<span class="hljs-comment">// Prints: &#x3C;Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1652
1653<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>(<span class="hljs-string">'this is a test'</span>));
1654<span class="hljs-comment">// Prints: &#x3C;Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74></span></code><button class="copy-button">copy</button></pre>
1655<p>For objects that support <code>Symbol.toPrimitive</code>, returns
1656<code>Buffer.from(object[Symbol.toPrimitive]('string'), offsetOrEncoding)</code>.</p>
1657
1658<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1659
1660<span class="hljs-keyword">class</span> <span class="hljs-title class_">Foo</span> {
1661  [<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toPrimitive</span>]() {
1662    <span class="hljs-keyword">return</span> <span class="hljs-string">'this is a test'</span>;
1663  }
1664}
1665
1666<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Foo</span>(), <span class="hljs-string">'utf8'</span>);
1667<span class="hljs-comment">// Prints: &#x3C;Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1668
1669<span class="hljs-keyword">class</span> <span class="hljs-title class_">Foo</span> {
1670  [<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toPrimitive</span>]() {
1671    <span class="hljs-keyword">return</span> <span class="hljs-string">'this is a test'</span>;
1672  }
1673}
1674
1675<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Foo</span>(), <span class="hljs-string">'utf8'</span>);
1676<span class="hljs-comment">// Prints: &#x3C;Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74></span></code><button class="copy-button">copy</button></pre>
1677<p>A <code>TypeError</code> will be thrown if <code>object</code> does not have the mentioned methods or
1678is not of another type appropriate for <code>Buffer.from()</code> variants.</p>
1679<h4>Static method: <code>Buffer.from(string[, encoding])</code><span><a class="mark" href="#static-method-bufferfromstring-encoding" id="static-method-bufferfromstring-encoding">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_from_string_encoding"></a></h4>
1680<div class="api_metadata">
1681<span>Added in: v5.10.0</span>
1682</div>
1683<ul>
1684<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> A string to encode.</li>
1685<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The encoding of <code>string</code>. <strong>Default:</strong> <code>'utf8'</code>.</li>
1686</ul>
1687<p>Creates a new <code>Buffer</code> containing <code>string</code>. The <code>encoding</code> parameter identifies
1688the character encoding to be used when converting <code>string</code> into bytes.</p>
1689
1690<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1691
1692<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'this is a tést'</span>);
1693<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'7468697320697320612074c3a97374'</span>, <span class="hljs-string">'hex'</span>);
1694
1695<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>());
1696<span class="hljs-comment">// Prints: this is a tést</span>
1697<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>());
1698<span class="hljs-comment">// Prints: this is a tést</span>
1699<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'latin1'</span>));
1700<span class="hljs-comment">// Prints: this is a tést</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1701
1702<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'this is a tést'</span>);
1703<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'7468697320697320612074c3a97374'</span>, <span class="hljs-string">'hex'</span>);
1704
1705<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>());
1706<span class="hljs-comment">// Prints: this is a tést</span>
1707<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>());
1708<span class="hljs-comment">// Prints: this is a tést</span>
1709<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'latin1'</span>));
1710<span class="hljs-comment">// Prints: this is a tést</span></code><button class="copy-button">copy</button></pre>
1711<p>A <code>TypeError</code> will be thrown if <code>string</code> is not a string or another type
1712appropriate for <code>Buffer.from()</code> variants.</p>
1713<h4>Static method: <code>Buffer.isBuffer(obj)</code><span><a class="mark" href="#static-method-bufferisbufferobj" id="static-method-bufferisbufferobj">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_isbuffer_obj"></a></h4>
1714<div class="api_metadata">
1715<span>Added in: v0.1.101</span>
1716</div>
1717<ul>
1718<li><code>obj</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
1719<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1720</ul>
1721<p>Returns <code>true</code> if <code>obj</code> is a <code>Buffer</code>, <code>false</code> otherwise.</p>
1722
1723<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1724
1725<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">10</span>)); <span class="hljs-comment">// true</span>
1726<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'foo'</span>)); <span class="hljs-comment">// true</span>
1727<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-string">'a string'</span>); <span class="hljs-comment">// false</span>
1728<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>([]); <span class="hljs-comment">// false</span>
1729<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(<span class="hljs-number">1024</span>)); <span class="hljs-comment">// false</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1730
1731<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">10</span>)); <span class="hljs-comment">// true</span>
1732<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'foo'</span>)); <span class="hljs-comment">// true</span>
1733<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-string">'a string'</span>); <span class="hljs-comment">// false</span>
1734<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>([]); <span class="hljs-comment">// false</span>
1735<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint8Array</span>(<span class="hljs-number">1024</span>)); <span class="hljs-comment">// false</span></code><button class="copy-button">copy</button></pre>
1736<h4>Static method: <code>Buffer.isEncoding(encoding)</code><span><a class="mark" href="#static-method-bufferisencodingencoding" id="static-method-bufferisencodingencoding">#</a></span><a aria-hidden="true" class="legacy" id="buffer_static_method_buffer_isencoding_encoding"></a></h4>
1737<div class="api_metadata">
1738<span>Added in: v0.9.1</span>
1739</div>
1740<ul>
1741<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> A character encoding name to check.</li>
1742<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1743</ul>
1744<p>Returns <code>true</code> if <code>encoding</code> is the name of a supported character encoding,
1745or <code>false</code> otherwise.</p>
1746
1747<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1748
1749<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isEncoding</span>(<span class="hljs-string">'utf8'</span>));
1750<span class="hljs-comment">// Prints: true</span>
1751
1752<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isEncoding</span>(<span class="hljs-string">'hex'</span>));
1753<span class="hljs-comment">// Prints: true</span>
1754
1755<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isEncoding</span>(<span class="hljs-string">'utf/8'</span>));
1756<span class="hljs-comment">// Prints: false</span>
1757
1758<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isEncoding</span>(<span class="hljs-string">''</span>));
1759<span class="hljs-comment">// Prints: false</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1760
1761<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isEncoding</span>(<span class="hljs-string">'utf8'</span>));
1762<span class="hljs-comment">// Prints: true</span>
1763
1764<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isEncoding</span>(<span class="hljs-string">'hex'</span>));
1765<span class="hljs-comment">// Prints: true</span>
1766
1767<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isEncoding</span>(<span class="hljs-string">'utf/8'</span>));
1768<span class="hljs-comment">// Prints: false</span>
1769
1770<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isEncoding</span>(<span class="hljs-string">''</span>));
1771<span class="hljs-comment">// Prints: false</span></code><button class="copy-button">copy</button></pre>
1772<h4>Class property: <code>Buffer.poolSize</code><span><a class="mark" href="#class-property-bufferpoolsize" id="class-property-bufferpoolsize">#</a></span><a aria-hidden="true" class="legacy" id="buffer_class_property_buffer_poolsize"></a></h4>
1773<div class="api_metadata">
1774<span>Added in: v0.11.3</span>
1775</div>
1776<ul>
1777<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>8192</code></li>
1778</ul>
1779<p>This is the size (in bytes) of pre-allocated internal <code>Buffer</code> instances used
1780for pooling. This value may be modified.</p>
1781<h4><code>buf[index]</code><span><a class="mark" href="#bufindex" id="bufindex">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_index"></a></h4>
1782<ul>
1783<li><code>index</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1784</ul>
1785<p>The index operator <code>[index]</code> can be used to get and set the octet at position
1786<code>index</code> in <code>buf</code>. The values refer to individual bytes, so the legal value
1787range is between <code>0x00</code> and <code>0xFF</code> (hex) or <code>0</code> and <code>255</code> (decimal).</p>
1788<p>This operator is inherited from <code>Uint8Array</code>, so its behavior on out-of-bounds
1789access is the same as <code>Uint8Array</code>. In other words, <code>buf[index]</code> returns
1790<code>undefined</code> when <code>index</code> is negative or greater or equal to <code>buf.length</code>, and
1791<code>buf[index] = value</code> does not modify the buffer if <code>index</code> is negative or
1792<code>>= buf.length</code>.</p>
1793
1794<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1795
1796<span class="hljs-comment">// Copy an ASCII string into a `Buffer` one byte at a time.</span>
1797<span class="hljs-comment">// (This only works for ASCII-only strings. In general, one should use</span>
1798<span class="hljs-comment">// `Buffer.from()` to perform this conversion.)</span>
1799
1800<span class="hljs-keyword">const</span> str = <span class="hljs-string">'Node.js'</span>;
1801<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(str.<span class="hljs-property">length</span>);
1802
1803<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &#x3C; str.<span class="hljs-property">length</span>; i++) {
1804  buf[i] = str.<span class="hljs-title function_">charCodeAt</span>(i);
1805}
1806
1807<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>));
1808<span class="hljs-comment">// Prints: Node.js</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1809
1810<span class="hljs-comment">// Copy an ASCII string into a `Buffer` one byte at a time.</span>
1811<span class="hljs-comment">// (This only works for ASCII-only strings. In general, one should use</span>
1812<span class="hljs-comment">// `Buffer.from()` to perform this conversion.)</span>
1813
1814<span class="hljs-keyword">const</span> str = <span class="hljs-string">'Node.js'</span>;
1815<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(str.<span class="hljs-property">length</span>);
1816
1817<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &#x3C; str.<span class="hljs-property">length</span>; i++) {
1818  buf[i] = str.<span class="hljs-title function_">charCodeAt</span>(i);
1819}
1820
1821<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>));
1822<span class="hljs-comment">// Prints: Node.js</span></code><button class="copy-button">copy</button></pre>
1823<h4><code>buf.buffer</code><span><a class="mark" href="#bufbuffer" id="bufbuffer">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_buffer"></a></h4>
1824<ul>
1825<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a> The underlying <code>ArrayBuffer</code> object based on which this <code>Buffer</code>
1826object is created.</li>
1827</ul>
1828<p>This <code>ArrayBuffer</code> is not guaranteed to correspond exactly to the original
1829<code>Buffer</code>. See the notes on <code>buf.byteOffset</code> for details.</p>
1830
1831<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1832
1833<span class="hljs-keyword">const</span> arrayBuffer = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>(<span class="hljs-number">16</span>);
1834<span class="hljs-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arrayBuffer);
1835
1836<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buffer.<span class="hljs-property">buffer</span> === arrayBuffer);
1837<span class="hljs-comment">// Prints: true</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1838
1839<span class="hljs-keyword">const</span> arrayBuffer = <span class="hljs-keyword">new</span> <span class="hljs-title class_">ArrayBuffer</span>(<span class="hljs-number">16</span>);
1840<span class="hljs-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arrayBuffer);
1841
1842<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buffer.<span class="hljs-property">buffer</span> === arrayBuffer);
1843<span class="hljs-comment">// Prints: true</span></code><button class="copy-button">copy</button></pre>
1844<h4><code>buf.byteOffset</code><span><a class="mark" href="#bufbyteoffset" id="bufbyteoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_byteoffset"></a></h4>
1845<ul>
1846<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The <code>byteOffset</code> of the <code>Buffer</code>s underlying <code>ArrayBuffer</code> object.</li>
1847</ul>
1848<p>When setting <code>byteOffset</code> in <code>Buffer.from(ArrayBuffer, byteOffset, length)</code>,
1849or sometimes when allocating a <code>Buffer</code> smaller than <code>Buffer.poolSize</code>, the
1850buffer does not start from a zero offset on the underlying <code>ArrayBuffer</code>.</p>
1851<p>This can cause problems when accessing the underlying <code>ArrayBuffer</code> directly
1852using <code>buf.buffer</code>, as other parts of the <code>ArrayBuffer</code> may be unrelated
1853to the <code>Buffer</code> object itself.</p>
1854<p>A common issue when creating a <code>TypedArray</code> object that shares its memory with
1855a <code>Buffer</code> is that in this case one needs to specify the <code>byteOffset</code> correctly:</p>
1856
1857<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1858
1859<span class="hljs-comment">// Create a buffer smaller than `Buffer.poolSize`.</span>
1860<span class="hljs-keyword">const</span> nodeBuffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>]);
1861
1862<span class="hljs-comment">// When casting the Node.js Buffer to an Int8Array, use the byteOffset</span>
1863<span class="hljs-comment">// to refer only to the part of `nodeBuffer.buffer` that contains the memory</span>
1864<span class="hljs-comment">// for `nodeBuffer`.</span>
1865<span class="hljs-keyword">new</span> <span class="hljs-title class_">Int8Array</span>(nodeBuffer.<span class="hljs-property">buffer</span>, nodeBuffer.<span class="hljs-property">byteOffset</span>, nodeBuffer.<span class="hljs-property">length</span>);</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1866
1867<span class="hljs-comment">// Create a buffer smaller than `Buffer.poolSize`.</span>
1868<span class="hljs-keyword">const</span> nodeBuffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>]);
1869
1870<span class="hljs-comment">// When casting the Node.js Buffer to an Int8Array, use the byteOffset</span>
1871<span class="hljs-comment">// to refer only to the part of `nodeBuffer.buffer` that contains the memory</span>
1872<span class="hljs-comment">// for `nodeBuffer`.</span>
1873<span class="hljs-keyword">new</span> <span class="hljs-title class_">Int8Array</span>(nodeBuffer.<span class="hljs-property">buffer</span>, nodeBuffer.<span class="hljs-property">byteOffset</span>, nodeBuffer.<span class="hljs-property">length</span>);</code><button class="copy-button">copy</button></pre>
1874<h4><code>buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])</code><span><a class="mark" href="#bufcomparetarget-targetstart-targetend-sourcestart-sourceend" id="bufcomparetarget-targetstart-targetend-sourcestart-sourceend">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_compare_target_targetstart_targetend_sourcestart_sourceend"></a></h4>
1875<div class="api_metadata">
1876<details class="changelog"><summary>History</summary>
1877<table>
1878<tbody><tr><th>Version</th><th>Changes</th></tr>
1879<tr><td>v8.0.0</td>
1880<td><p>The <code>target</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
1881<tr><td>v5.11.0</td>
1882<td><p>Additional parameters for specifying offsets are supported now.</p></td></tr>
1883<tr><td>v0.11.13</td>
1884<td><p><span>Added in: v0.11.13</span></p></td></tr>
1885</tbody></table>
1886</details>
1887</div>
1888<ul>
1889<li><code>target</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> A <code>Buffer</code> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> with which to
1890compare <code>buf</code>.</li>
1891<li><code>targetStart</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The offset within <code>target</code> at which to begin
1892comparison. <strong>Default:</strong> <code>0</code>.</li>
1893<li><code>targetEnd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The offset within <code>target</code> at which to end comparison
1894(not inclusive). <strong>Default:</strong> <code>target.length</code>.</li>
1895<li><code>sourceStart</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The offset within <code>buf</code> at which to begin comparison.
1896<strong>Default:</strong> <code>0</code>.</li>
1897<li><code>sourceEnd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The offset within <code>buf</code> at which to end comparison
1898(not inclusive). <strong>Default:</strong> <a href="#buflength"><code>buf.length</code></a>.</li>
1899<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1900</ul>
1901<p>Compares <code>buf</code> with <code>target</code> and returns a number indicating whether <code>buf</code>
1902comes before, after, or is the same as <code>target</code> in sort order.
1903Comparison is based on the actual sequence of bytes in each <code>Buffer</code>.</p>
1904<ul>
1905<li><code>0</code> is returned if <code>target</code> is the same as <code>buf</code></li>
1906<li><code>1</code> is returned if <code>target</code> should come <em>before</em> <code>buf</code> when sorted.</li>
1907<li><code>-1</code> is returned if <code>target</code> should come <em>after</em> <code>buf</code> when sorted.</li>
1908</ul>
1909
1910<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1911
1912<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'ABC'</span>);
1913<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'BCD'</span>);
1914<span class="hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'ABCD'</span>);
1915
1916<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf1));
1917<span class="hljs-comment">// Prints: 0</span>
1918<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf2));
1919<span class="hljs-comment">// Prints: -1</span>
1920<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf3));
1921<span class="hljs-comment">// Prints: -1</span>
1922<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">compare</span>(buf1));
1923<span class="hljs-comment">// Prints: 1</span>
1924<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">compare</span>(buf3));
1925<span class="hljs-comment">// Prints: 1</span>
1926<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>([buf1, buf2, buf3].<span class="hljs-title function_">sort</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-property">compare</span>));
1927<span class="hljs-comment">// Prints: [ &#x3C;Buffer 41 42 43>, &#x3C;Buffer 41 42 43 44>, &#x3C;Buffer 42 43 44> ]</span>
1928<span class="hljs-comment">// (This result is equal to: [buf1, buf3, buf2].)</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1929
1930<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'ABC'</span>);
1931<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'BCD'</span>);
1932<span class="hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'ABCD'</span>);
1933
1934<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf1));
1935<span class="hljs-comment">// Prints: 0</span>
1936<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf2));
1937<span class="hljs-comment">// Prints: -1</span>
1938<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf3));
1939<span class="hljs-comment">// Prints: -1</span>
1940<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">compare</span>(buf1));
1941<span class="hljs-comment">// Prints: 1</span>
1942<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">compare</span>(buf3));
1943<span class="hljs-comment">// Prints: 1</span>
1944<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>([buf1, buf2, buf3].<span class="hljs-title function_">sort</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-property">compare</span>));
1945<span class="hljs-comment">// Prints: [ &#x3C;Buffer 41 42 43>, &#x3C;Buffer 41 42 43 44>, &#x3C;Buffer 42 43 44> ]</span>
1946<span class="hljs-comment">// (This result is equal to: [buf1, buf3, buf2].)</span></code><button class="copy-button">copy</button></pre>
1947<p>The optional <code>targetStart</code>, <code>targetEnd</code>, <code>sourceStart</code>, and <code>sourceEnd</code>
1948arguments can be used to limit the comparison to specific ranges within <code>target</code>
1949and <code>buf</code> respectively.</p>
1950
1951<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1952
1953<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>]);
1954<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]);
1955
1956<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf2, <span class="hljs-number">5</span>, <span class="hljs-number">9</span>, <span class="hljs-number">0</span>, <span class="hljs-number">4</span>));
1957<span class="hljs-comment">// Prints: 0</span>
1958<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf2, <span class="hljs-number">0</span>, <span class="hljs-number">6</span>, <span class="hljs-number">4</span>));
1959<span class="hljs-comment">// Prints: -1</span>
1960<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf2, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">5</span>));
1961<span class="hljs-comment">// Prints: 1</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
1962
1963<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>]);
1964<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]);
1965
1966<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf2, <span class="hljs-number">5</span>, <span class="hljs-number">9</span>, <span class="hljs-number">0</span>, <span class="hljs-number">4</span>));
1967<span class="hljs-comment">// Prints: 0</span>
1968<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf2, <span class="hljs-number">0</span>, <span class="hljs-number">6</span>, <span class="hljs-number">4</span>));
1969<span class="hljs-comment">// Prints: -1</span>
1970<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf2, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">5</span>));
1971<span class="hljs-comment">// Prints: 1</span></code><button class="copy-button">copy</button></pre>
1972<p><a href="errors.html#err_out_of_range"><code>ERR_OUT_OF_RANGE</code></a> is thrown if <code>targetStart &#x3C; 0</code>, <code>sourceStart &#x3C; 0</code>,
1973<code>targetEnd > target.byteLength</code>, or <code>sourceEnd > source.byteLength</code>.</p>
1974<h4><code>buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])</code><span><a class="mark" href="#bufcopytarget-targetstart-sourcestart-sourceend" id="bufcopytarget-targetstart-sourcestart-sourceend">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_copy_target_targetstart_sourcestart_sourceend"></a></h4>
1975<div class="api_metadata">
1976<span>Added in: v0.1.90</span>
1977</div>
1978<ul>
1979<li><code>target</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> A <code>Buffer</code> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> to copy into.</li>
1980<li><code>targetStart</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The offset within <code>target</code> at which to begin
1981writing. <strong>Default:</strong> <code>0</code>.</li>
1982<li><code>sourceStart</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The offset within <code>buf</code> from which to begin copying.
1983<strong>Default:</strong> <code>0</code>.</li>
1984<li><code>sourceEnd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The offset within <code>buf</code> at which to stop copying (not
1985inclusive). <strong>Default:</strong> <a href="#buflength"><code>buf.length</code></a>.</li>
1986<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The number of bytes copied.</li>
1987</ul>
1988<p>Copies data from a region of <code>buf</code> to a region in <code>target</code>, even if the <code>target</code>
1989memory region overlaps with <code>buf</code>.</p>
1990<p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/set"><code>TypedArray.prototype.set()</code></a> performs the same operation, and is available
1991for all TypedArrays, including Node.js <code>Buffer</code>s, although it takes
1992different function arguments.</p>
1993
1994<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
1995
1996<span class="hljs-comment">// Create two `Buffer` instances.</span>
1997<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">26</span>);
1998<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">26</span>).<span class="hljs-title function_">fill</span>(<span class="hljs-string">'!'</span>);
1999
2000<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &#x3C; <span class="hljs-number">26</span>; i++) {
2001  <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
2002  buf1[i] = i + <span class="hljs-number">97</span>;
2003}
2004
2005<span class="hljs-comment">// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`.</span>
2006buf1.<span class="hljs-title function_">copy</span>(buf2, <span class="hljs-number">8</span>, <span class="hljs-number">16</span>, <span class="hljs-number">20</span>);
2007<span class="hljs-comment">// This is equivalent to:</span>
2008<span class="hljs-comment">// buf2.set(buf1.subarray(16, 20), 8);</span>
2009
2010<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'ascii'</span>, <span class="hljs-number">0</span>, <span class="hljs-number">25</span>));
2011<span class="hljs-comment">// Prints: !!!!!!!!qrst!!!!!!!!!!!!!</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2012
2013<span class="hljs-comment">// Create two `Buffer` instances.</span>
2014<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">26</span>);
2015<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">26</span>).<span class="hljs-title function_">fill</span>(<span class="hljs-string">'!'</span>);
2016
2017<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &#x3C; <span class="hljs-number">26</span>; i++) {
2018  <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
2019  buf1[i] = i + <span class="hljs-number">97</span>;
2020}
2021
2022<span class="hljs-comment">// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`.</span>
2023buf1.<span class="hljs-title function_">copy</span>(buf2, <span class="hljs-number">8</span>, <span class="hljs-number">16</span>, <span class="hljs-number">20</span>);
2024<span class="hljs-comment">// This is equivalent to:</span>
2025<span class="hljs-comment">// buf2.set(buf1.subarray(16, 20), 8);</span>
2026
2027<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'ascii'</span>, <span class="hljs-number">0</span>, <span class="hljs-number">25</span>));
2028<span class="hljs-comment">// Prints: !!!!!!!!qrst!!!!!!!!!!!!!</span></code><button class="copy-button">copy</button></pre>
2029
2030<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2031
2032<span class="hljs-comment">// Create a `Buffer` and copy data from one region to an overlapping region</span>
2033<span class="hljs-comment">// within the same `Buffer`.</span>
2034
2035<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">26</span>);
2036
2037<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &#x3C; <span class="hljs-number">26</span>; i++) {
2038  <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
2039  buf[i] = i + <span class="hljs-number">97</span>;
2040}
2041
2042buf.<span class="hljs-title function_">copy</span>(buf, <span class="hljs-number">0</span>, <span class="hljs-number">4</span>, <span class="hljs-number">10</span>);
2043
2044<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>());
2045<span class="hljs-comment">// Prints: efghijghijklmnopqrstuvwxyz</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2046
2047<span class="hljs-comment">// Create a `Buffer` and copy data from one region to an overlapping region</span>
2048<span class="hljs-comment">// within the same `Buffer`.</span>
2049
2050<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">26</span>);
2051
2052<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &#x3C; <span class="hljs-number">26</span>; i++) {
2053  <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
2054  buf[i] = i + <span class="hljs-number">97</span>;
2055}
2056
2057buf.<span class="hljs-title function_">copy</span>(buf, <span class="hljs-number">0</span>, <span class="hljs-number">4</span>, <span class="hljs-number">10</span>);
2058
2059<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>());
2060<span class="hljs-comment">// Prints: efghijghijklmnopqrstuvwxyz</span></code><button class="copy-button">copy</button></pre>
2061<h4><code>buf.entries()</code><span><a class="mark" href="#bufentries" id="bufentries">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_entries"></a></h4>
2062<div class="api_metadata">
2063<span>Added in: v1.1.0</span>
2064</div>
2065<ul>
2066<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type">&#x3C;Iterator></a></li>
2067</ul>
2068<p>Creates and returns an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols">iterator</a> of <code>[index, byte]</code> pairs from the contents
2069of <code>buf</code>.</p>
2070
2071<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2072
2073<span class="hljs-comment">// Log the entire contents of a `Buffer`.</span>
2074
2075<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>);
2076
2077<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> pair <span class="hljs-keyword">of</span> buf.<span class="hljs-title function_">entries</span>()) {
2078  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(pair);
2079}
2080<span class="hljs-comment">// Prints:</span>
2081<span class="hljs-comment">//   [0, 98]</span>
2082<span class="hljs-comment">//   [1, 117]</span>
2083<span class="hljs-comment">//   [2, 102]</span>
2084<span class="hljs-comment">//   [3, 102]</span>
2085<span class="hljs-comment">//   [4, 101]</span>
2086<span class="hljs-comment">//   [5, 114]</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2087
2088<span class="hljs-comment">// Log the entire contents of a `Buffer`.</span>
2089
2090<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>);
2091
2092<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> pair <span class="hljs-keyword">of</span> buf.<span class="hljs-title function_">entries</span>()) {
2093  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(pair);
2094}
2095<span class="hljs-comment">// Prints:</span>
2096<span class="hljs-comment">//   [0, 98]</span>
2097<span class="hljs-comment">//   [1, 117]</span>
2098<span class="hljs-comment">//   [2, 102]</span>
2099<span class="hljs-comment">//   [3, 102]</span>
2100<span class="hljs-comment">//   [4, 101]</span>
2101<span class="hljs-comment">//   [5, 114]</span></code><button class="copy-button">copy</button></pre>
2102<h4><code>buf.equals(otherBuffer)</code><span><a class="mark" href="#bufequalsotherbuffer" id="bufequalsotherbuffer">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_equals_otherbuffer"></a></h4>
2103<div class="api_metadata">
2104<details class="changelog"><summary>History</summary>
2105<table>
2106<tbody><tr><th>Version</th><th>Changes</th></tr>
2107<tr><td>v8.0.0</td>
2108<td><p>The arguments can now be <code>Uint8Array</code>s.</p></td></tr>
2109<tr><td>v0.11.13</td>
2110<td><p><span>Added in: v0.11.13</span></p></td></tr>
2111</tbody></table>
2112</details>
2113</div>
2114<ul>
2115<li><code>otherBuffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> A <code>Buffer</code> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> with which to
2116compare <code>buf</code>.</li>
2117<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
2118</ul>
2119<p>Returns <code>true</code> if both <code>buf</code> and <code>otherBuffer</code> have exactly the same bytes,
2120<code>false</code> otherwise. Equivalent to
2121<a href="#bufcomparetarget-targetstart-targetend-sourcestart-sourceend"><code>buf.compare(otherBuffer) === 0</code></a>.</p>
2122
2123<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2124
2125<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'ABC'</span>);
2126<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'414243'</span>, <span class="hljs-string">'hex'</span>);
2127<span class="hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'ABCD'</span>);
2128
2129<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">equals</span>(buf2));
2130<span class="hljs-comment">// Prints: true</span>
2131<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">equals</span>(buf3));
2132<span class="hljs-comment">// Prints: false</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2133
2134<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'ABC'</span>);
2135<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'414243'</span>, <span class="hljs-string">'hex'</span>);
2136<span class="hljs-keyword">const</span> buf3 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'ABCD'</span>);
2137
2138<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">equals</span>(buf2));
2139<span class="hljs-comment">// Prints: true</span>
2140<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">equals</span>(buf3));
2141<span class="hljs-comment">// Prints: false</span></code><button class="copy-button">copy</button></pre>
2142<h4><code>buf.fill(value[, offset[, end]][, encoding])</code><span><a class="mark" href="#buffillvalue-offset-end-encoding" id="buffillvalue-offset-end-encoding">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_fill_value_offset_end_encoding"></a></h4>
2143<div class="api_metadata">
2144<details class="changelog"><summary>History</summary>
2145<table>
2146<tbody><tr><th>Version</th><th>Changes</th></tr>
2147<tr><td>v11.0.0</td>
2148<td><p>Throws <code>ERR_OUT_OF_RANGE</code> instead of <code>ERR_INDEX_OUT_OF_RANGE</code>.</p></td></tr>
2149<tr><td>v10.0.0</td>
2150<td><p>Negative <code>end</code> values throw an <code>ERR_INDEX_OUT_OF_RANGE</code> error.</p></td></tr>
2151<tr><td>v10.0.0</td>
2152<td><p>Attempting to fill a non-zero length buffer with a zero length buffer triggers a thrown exception.</p></td></tr>
2153<tr><td>v10.0.0</td>
2154<td><p>Specifying an invalid string for <code>value</code> triggers a thrown exception.</p></td></tr>
2155<tr><td>v5.7.0</td>
2156<td><p>The <code>encoding</code> parameter is supported now.</p></td></tr>
2157<tr><td>v0.5.0</td>
2158<td><p><span>Added in: v0.5.0</span></p></td></tr>
2159</tbody></table>
2160</details>
2161</div>
2162<ul>
2163<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The value with which to fill <code>buf</code>.
2164Empty value (string, Uint8Array, Buffer) is coerced to <code>0</code>.</li>
2165<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to fill <code>buf</code>.
2166<strong>Default:</strong> <code>0</code>.</li>
2167<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Where to stop filling <code>buf</code> (not inclusive). <strong>Default:</strong>
2168<a href="#buflength"><code>buf.length</code></a>.</li>
2169<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The encoding for <code>value</code> if <code>value</code> is a string.
2170<strong>Default:</strong> <code>'utf8'</code>.</li>
2171<li>Returns: <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> A reference to <code>buf</code>.</li>
2172</ul>
2173<p>Fills <code>buf</code> with the specified <code>value</code>. If the <code>offset</code> and <code>end</code> are not given,
2174the entire <code>buf</code> will be filled:</p>
2175
2176<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2177
2178<span class="hljs-comment">// Fill a `Buffer` with the ASCII character 'h'.</span>
2179
2180<span class="hljs-keyword">const</span> b = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">50</span>).<span class="hljs-title function_">fill</span>(<span class="hljs-string">'h'</span>);
2181
2182<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">toString</span>());
2183<span class="hljs-comment">// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh</span>
2184
2185<span class="hljs-comment">// Fill a buffer with empty string</span>
2186<span class="hljs-keyword">const</span> c = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">5</span>).<span class="hljs-title function_">fill</span>(<span class="hljs-string">''</span>);
2187
2188<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(c.<span class="hljs-title function_">fill</span>(<span class="hljs-string">''</span>));
2189<span class="hljs-comment">// Prints: &#x3C;Buffer 00 00 00 00 00></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2190
2191<span class="hljs-comment">// Fill a `Buffer` with the ASCII character 'h'.</span>
2192
2193<span class="hljs-keyword">const</span> b = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">50</span>).<span class="hljs-title function_">fill</span>(<span class="hljs-string">'h'</span>);
2194
2195<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">toString</span>());
2196<span class="hljs-comment">// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh</span>
2197
2198<span class="hljs-comment">// Fill a buffer with empty string</span>
2199<span class="hljs-keyword">const</span> c = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">5</span>).<span class="hljs-title function_">fill</span>(<span class="hljs-string">''</span>);
2200
2201<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(c.<span class="hljs-title function_">fill</span>(<span class="hljs-string">''</span>));
2202<span class="hljs-comment">// Prints: &#x3C;Buffer 00 00 00 00 00></span></code><button class="copy-button">copy</button></pre>
2203<p><code>value</code> is coerced to a <code>uint32</code> value if it is not a string, <code>Buffer</code>, or
2204integer. If the resulting integer is greater than <code>255</code> (decimal), <code>buf</code> will be
2205filled with <code>value &#x26; 255</code>.</p>
2206<p>If the final write of a <code>fill()</code> operation falls on a multi-byte character,
2207then only the bytes of that character that fit into <code>buf</code> are written:</p>
2208
2209<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2210
2211<span class="hljs-comment">// Fill a `Buffer` with character that takes up two bytes in UTF-8.</span>
2212
2213<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">5</span>).<span class="hljs-title function_">fill</span>(<span class="hljs-string">'\u0222'</span>));
2214<span class="hljs-comment">// Prints: &#x3C;Buffer c8 a2 c8 a2 c8></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2215
2216<span class="hljs-comment">// Fill a `Buffer` with character that takes up two bytes in UTF-8.</span>
2217
2218<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">5</span>).<span class="hljs-title function_">fill</span>(<span class="hljs-string">'\u0222'</span>));
2219<span class="hljs-comment">// Prints: &#x3C;Buffer c8 a2 c8 a2 c8></span></code><button class="copy-button">copy</button></pre>
2220<p>If <code>value</code> contains invalid characters, it is truncated; if no valid
2221fill data remains, an exception is thrown:</p>
2222
2223<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2224
2225<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">5</span>);
2226
2227<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">fill</span>(<span class="hljs-string">'a'</span>));
2228<span class="hljs-comment">// Prints: &#x3C;Buffer 61 61 61 61 61></span>
2229<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">fill</span>(<span class="hljs-string">'aazz'</span>, <span class="hljs-string">'hex'</span>));
2230<span class="hljs-comment">// Prints: &#x3C;Buffer aa aa aa aa aa></span>
2231<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">fill</span>(<span class="hljs-string">'zz'</span>, <span class="hljs-string">'hex'</span>));
2232<span class="hljs-comment">// Throws an exception.</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2233
2234<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">5</span>);
2235
2236<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">fill</span>(<span class="hljs-string">'a'</span>));
2237<span class="hljs-comment">// Prints: &#x3C;Buffer 61 61 61 61 61></span>
2238<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">fill</span>(<span class="hljs-string">'aazz'</span>, <span class="hljs-string">'hex'</span>));
2239<span class="hljs-comment">// Prints: &#x3C;Buffer aa aa aa aa aa></span>
2240<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">fill</span>(<span class="hljs-string">'zz'</span>, <span class="hljs-string">'hex'</span>));
2241<span class="hljs-comment">// Throws an exception.</span></code><button class="copy-button">copy</button></pre>
2242<h4><code>buf.includes(value[, byteOffset][, encoding])</code><span><a class="mark" href="#bufincludesvalue-byteoffset-encoding" id="bufincludesvalue-byteoffset-encoding">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_includes_value_byteoffset_encoding"></a></h4>
2243<div class="api_metadata">
2244<span>Added in: v5.3.0</span>
2245</div>
2246<ul>
2247<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> What to search for.</li>
2248<li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Where to begin searching in <code>buf</code>. If negative, then
2249offset is calculated from the end of <code>buf</code>. <strong>Default:</strong> <code>0</code>.</li>
2250<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> If <code>value</code> is a string, this is its encoding.
2251<strong>Default:</strong> <code>'utf8'</code>.</li>
2252<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> if <code>value</code> was found in <code>buf</code>, <code>false</code> otherwise.</li>
2253</ul>
2254<p>Equivalent to <a href="#bufindexofvalue-byteoffset-encoding"><code>buf.indexOf() !== -1</code></a>.</p>
2255
2256<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2257
2258<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'this is a buffer'</span>);
2259
2260<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-string">'this'</span>));
2261<span class="hljs-comment">// Prints: true</span>
2262<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-string">'is'</span>));
2263<span class="hljs-comment">// Prints: true</span>
2264<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer'</span>)));
2265<span class="hljs-comment">// Prints: true</span>
2266<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-number">97</span>));
2267<span class="hljs-comment">// Prints: true (97 is the decimal ASCII value for 'a')</span>
2268<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer example'</span>)));
2269<span class="hljs-comment">// Prints: false</span>
2270<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer example'</span>).<span class="hljs-title function_">slice</span>(<span class="hljs-number">0</span>, <span class="hljs-number">8</span>)));
2271<span class="hljs-comment">// Prints: true</span>
2272<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-string">'this'</span>, <span class="hljs-number">4</span>));
2273<span class="hljs-comment">// Prints: false</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2274
2275<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'this is a buffer'</span>);
2276
2277<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-string">'this'</span>));
2278<span class="hljs-comment">// Prints: true</span>
2279<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-string">'is'</span>));
2280<span class="hljs-comment">// Prints: true</span>
2281<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer'</span>)));
2282<span class="hljs-comment">// Prints: true</span>
2283<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-number">97</span>));
2284<span class="hljs-comment">// Prints: true (97 is the decimal ASCII value for 'a')</span>
2285<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer example'</span>)));
2286<span class="hljs-comment">// Prints: false</span>
2287<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer example'</span>).<span class="hljs-title function_">slice</span>(<span class="hljs-number">0</span>, <span class="hljs-number">8</span>)));
2288<span class="hljs-comment">// Prints: true</span>
2289<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">includes</span>(<span class="hljs-string">'this'</span>, <span class="hljs-number">4</span>));
2290<span class="hljs-comment">// Prints: false</span></code><button class="copy-button">copy</button></pre>
2291<h4><code>buf.indexOf(value[, byteOffset][, encoding])</code><span><a class="mark" href="#bufindexofvalue-byteoffset-encoding" id="bufindexofvalue-byteoffset-encoding">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_indexof_value_byteoffset_encoding"></a></h4>
2292<div class="api_metadata">
2293<details class="changelog"><summary>History</summary>
2294<table>
2295<tbody><tr><th>Version</th><th>Changes</th></tr>
2296<tr><td>v8.0.0</td>
2297<td><p>The <code>value</code> can now be a <code>Uint8Array</code>.</p></td></tr>
2298<tr><td>v5.7.0, v4.4.0</td>
2299<td><p>When <code>encoding</code> is being passed, the <code>byteOffset</code> parameter is no longer required.</p></td></tr>
2300<tr><td>v1.5.0</td>
2301<td><p><span>Added in: v1.5.0</span></p></td></tr>
2302</tbody></table>
2303</details>
2304</div>
2305<ul>
2306<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> What to search for.</li>
2307<li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Where to begin searching in <code>buf</code>. If negative, then
2308offset is calculated from the end of <code>buf</code>. <strong>Default:</strong> <code>0</code>.</li>
2309<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> If <code>value</code> is a string, this is the encoding used to
2310determine the binary representation of the string that will be searched for in
2311<code>buf</code>. <strong>Default:</strong> <code>'utf8'</code>.</li>
2312<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The index of the first occurrence of <code>value</code> in <code>buf</code>, or
2313<code>-1</code> if <code>buf</code> does not contain <code>value</code>.</li>
2314</ul>
2315<p>If <code>value</code> is:</p>
2316<ul>
2317<li>a string, <code>value</code> is interpreted according to the character encoding in
2318<code>encoding</code>.</li>
2319<li>a <code>Buffer</code> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a>, <code>value</code> will be used in its entirety.
2320To compare a partial <code>Buffer</code>, use <a href="#bufsubarraystart-end"><code>buf.subarray</code></a>.</li>
2321<li>a number, <code>value</code> will be interpreted as an unsigned 8-bit integer
2322value between <code>0</code> and <code>255</code>.</li>
2323</ul>
2324
2325<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2326
2327<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'this is a buffer'</span>);
2328
2329<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'this'</span>));
2330<span class="hljs-comment">// Prints: 0</span>
2331<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'is'</span>));
2332<span class="hljs-comment">// Prints: 2</span>
2333<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer'</span>)));
2334<span class="hljs-comment">// Prints: 8</span>
2335<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-number">97</span>));
2336<span class="hljs-comment">// Prints: 8 (97 is the decimal ASCII value for 'a')</span>
2337<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer example'</span>)));
2338<span class="hljs-comment">// Prints: -1</span>
2339<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer example'</span>).<span class="hljs-title function_">slice</span>(<span class="hljs-number">0</span>, <span class="hljs-number">8</span>)));
2340<span class="hljs-comment">// Prints: 8</span>
2341
2342<span class="hljs-keyword">const</span> utf16Buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'\u039a\u0391\u03a3\u03a3\u0395'</span>, <span class="hljs-string">'utf16le'</span>);
2343
2344<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'\u03a3'</span>, <span class="hljs-number">0</span>, <span class="hljs-string">'utf16le'</span>));
2345<span class="hljs-comment">// Prints: 4</span>
2346<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'\u03a3'</span>, -<span class="hljs-number">4</span>, <span class="hljs-string">'utf16le'</span>));
2347<span class="hljs-comment">// Prints: 6</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2348
2349<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'this is a buffer'</span>);
2350
2351<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'this'</span>));
2352<span class="hljs-comment">// Prints: 0</span>
2353<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'is'</span>));
2354<span class="hljs-comment">// Prints: 2</span>
2355<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer'</span>)));
2356<span class="hljs-comment">// Prints: 8</span>
2357<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-number">97</span>));
2358<span class="hljs-comment">// Prints: 8 (97 is the decimal ASCII value for 'a')</span>
2359<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer example'</span>)));
2360<span class="hljs-comment">// Prints: -1</span>
2361<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">indexOf</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'a buffer example'</span>).<span class="hljs-title function_">slice</span>(<span class="hljs-number">0</span>, <span class="hljs-number">8</span>)));
2362<span class="hljs-comment">// Prints: 8</span>
2363
2364<span class="hljs-keyword">const</span> utf16Buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'\u039a\u0391\u03a3\u03a3\u0395'</span>, <span class="hljs-string">'utf16le'</span>);
2365
2366<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'\u03a3'</span>, <span class="hljs-number">0</span>, <span class="hljs-string">'utf16le'</span>));
2367<span class="hljs-comment">// Prints: 4</span>
2368<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'\u03a3'</span>, -<span class="hljs-number">4</span>, <span class="hljs-string">'utf16le'</span>));
2369<span class="hljs-comment">// Prints: 6</span></code><button class="copy-button">copy</button></pre>
2370<p>If <code>value</code> is not a string, number, or <code>Buffer</code>, this method will throw a
2371<code>TypeError</code>. If <code>value</code> is a number, it will be coerced to a valid byte value,
2372an integer between 0 and 255.</p>
2373<p>If <code>byteOffset</code> is not a number, it will be coerced to a number. If the result
2374of coercion is <code>NaN</code> or <code>0</code>, then the entire buffer will be searched. This
2375behavior matches <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf"><code>String.prototype.indexOf()</code></a>.</p>
2376
2377<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2378
2379<span class="hljs-keyword">const</span> b = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'abcdef'</span>);
2380
2381<span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span>
2382<span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span>
2383<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-number">99.9</span>));
2384<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-number">256</span> + <span class="hljs-number">99</span>));
2385
2386<span class="hljs-comment">// Passing a byteOffset that coerces to NaN or 0.</span>
2387<span class="hljs-comment">// Prints: 1, searching the whole buffer.</span>
2388<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'b'</span>, <span class="hljs-literal">undefined</span>));
2389<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'b'</span>, {}));
2390<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'b'</span>, <span class="hljs-literal">null</span>));
2391<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'b'</span>, []));</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2392
2393<span class="hljs-keyword">const</span> b = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'abcdef'</span>);
2394
2395<span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span>
2396<span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span>
2397<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-number">99.9</span>));
2398<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-number">256</span> + <span class="hljs-number">99</span>));
2399
2400<span class="hljs-comment">// Passing a byteOffset that coerces to NaN or 0.</span>
2401<span class="hljs-comment">// Prints: 1, searching the whole buffer.</span>
2402<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'b'</span>, <span class="hljs-literal">undefined</span>));
2403<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'b'</span>, {}));
2404<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'b'</span>, <span class="hljs-literal">null</span>));
2405<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">indexOf</span>(<span class="hljs-string">'b'</span>, []));</code><button class="copy-button">copy</button></pre>
2406<p>If <code>value</code> is an empty string or empty <code>Buffer</code> and <code>byteOffset</code> is less
2407than <code>buf.length</code>, <code>byteOffset</code> will be returned. If <code>value</code> is empty and
2408<code>byteOffset</code> is at least <code>buf.length</code>, <code>buf.length</code> will be returned.</p>
2409<h4><code>buf.keys()</code><span><a class="mark" href="#bufkeys" id="bufkeys">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_keys"></a></h4>
2410<div class="api_metadata">
2411<span>Added in: v1.1.0</span>
2412</div>
2413<ul>
2414<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type">&#x3C;Iterator></a></li>
2415</ul>
2416<p>Creates and returns an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols">iterator</a> of <code>buf</code> keys (indices).</p>
2417
2418<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2419
2420<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>);
2421
2422<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> key <span class="hljs-keyword">of</span> buf.<span class="hljs-title function_">keys</span>()) {
2423  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(key);
2424}
2425<span class="hljs-comment">// Prints:</span>
2426<span class="hljs-comment">//   0</span>
2427<span class="hljs-comment">//   1</span>
2428<span class="hljs-comment">//   2</span>
2429<span class="hljs-comment">//   3</span>
2430<span class="hljs-comment">//   4</span>
2431<span class="hljs-comment">//   5</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2432
2433<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>);
2434
2435<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> key <span class="hljs-keyword">of</span> buf.<span class="hljs-title function_">keys</span>()) {
2436  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(key);
2437}
2438<span class="hljs-comment">// Prints:</span>
2439<span class="hljs-comment">//   0</span>
2440<span class="hljs-comment">//   1</span>
2441<span class="hljs-comment">//   2</span>
2442<span class="hljs-comment">//   3</span>
2443<span class="hljs-comment">//   4</span>
2444<span class="hljs-comment">//   5</span></code><button class="copy-button">copy</button></pre>
2445<h4><code>buf.lastIndexOf(value[, byteOffset][, encoding])</code><span><a class="mark" href="#buflastindexofvalue-byteoffset-encoding" id="buflastindexofvalue-byteoffset-encoding">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_lastindexof_value_byteoffset_encoding"></a></h4>
2446<div class="api_metadata">
2447<details class="changelog"><summary>History</summary>
2448<table>
2449<tbody><tr><th>Version</th><th>Changes</th></tr>
2450<tr><td>v8.0.0</td>
2451<td><p>The <code>value</code> can now be a <code>Uint8Array</code>.</p></td></tr>
2452<tr><td>v6.0.0</td>
2453<td><p><span>Added in: v6.0.0</span></p></td></tr>
2454</tbody></table>
2455</details>
2456</div>
2457<ul>
2458<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> What to search for.</li>
2459<li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Where to begin searching in <code>buf</code>. If negative, then
2460offset is calculated from the end of <code>buf</code>. <strong>Default:</strong>
2461<code>buf.length - 1</code>.</li>
2462<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> If <code>value</code> is a string, this is the encoding used to
2463determine the binary representation of the string that will be searched for in
2464<code>buf</code>. <strong>Default:</strong> <code>'utf8'</code>.</li>
2465<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The index of the last occurrence of <code>value</code> in <code>buf</code>, or
2466<code>-1</code> if <code>buf</code> does not contain <code>value</code>.</li>
2467</ul>
2468<p>Identical to <a href="#bufindexofvalue-byteoffset-encoding"><code>buf.indexOf()</code></a>, except the last occurrence of <code>value</code> is found
2469rather than the first occurrence.</p>
2470
2471<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2472
2473<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'this buffer is a buffer'</span>);
2474
2475<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'this'</span>));
2476<span class="hljs-comment">// Prints: 0</span>
2477<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'buffer'</span>));
2478<span class="hljs-comment">// Prints: 17</span>
2479<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>)));
2480<span class="hljs-comment">// Prints: 17</span>
2481<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-number">97</span>));
2482<span class="hljs-comment">// Prints: 15 (97 is the decimal ASCII value for 'a')</span>
2483<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'yolo'</span>)));
2484<span class="hljs-comment">// Prints: -1</span>
2485<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'buffer'</span>, <span class="hljs-number">5</span>));
2486<span class="hljs-comment">// Prints: 5</span>
2487<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'buffer'</span>, <span class="hljs-number">4</span>));
2488<span class="hljs-comment">// Prints: -1</span>
2489
2490<span class="hljs-keyword">const</span> utf16Buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'\u039a\u0391\u03a3\u03a3\u0395'</span>, <span class="hljs-string">'utf16le'</span>);
2491
2492<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'\u03a3'</span>, <span class="hljs-literal">undefined</span>, <span class="hljs-string">'utf16le'</span>));
2493<span class="hljs-comment">// Prints: 6</span>
2494<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'\u03a3'</span>, -<span class="hljs-number">5</span>, <span class="hljs-string">'utf16le'</span>));
2495<span class="hljs-comment">// Prints: 4</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2496
2497<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'this buffer is a buffer'</span>);
2498
2499<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'this'</span>));
2500<span class="hljs-comment">// Prints: 0</span>
2501<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'buffer'</span>));
2502<span class="hljs-comment">// Prints: 17</span>
2503<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>)));
2504<span class="hljs-comment">// Prints: 17</span>
2505<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-number">97</span>));
2506<span class="hljs-comment">// Prints: 15 (97 is the decimal ASCII value for 'a')</span>
2507<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'yolo'</span>)));
2508<span class="hljs-comment">// Prints: -1</span>
2509<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'buffer'</span>, <span class="hljs-number">5</span>));
2510<span class="hljs-comment">// Prints: 5</span>
2511<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'buffer'</span>, <span class="hljs-number">4</span>));
2512<span class="hljs-comment">// Prints: -1</span>
2513
2514<span class="hljs-keyword">const</span> utf16Buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'\u039a\u0391\u03a3\u03a3\u0395'</span>, <span class="hljs-string">'utf16le'</span>);
2515
2516<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'\u03a3'</span>, <span class="hljs-literal">undefined</span>, <span class="hljs-string">'utf16le'</span>));
2517<span class="hljs-comment">// Prints: 6</span>
2518<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(utf16Buffer.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'\u03a3'</span>, -<span class="hljs-number">5</span>, <span class="hljs-string">'utf16le'</span>));
2519<span class="hljs-comment">// Prints: 4</span></code><button class="copy-button">copy</button></pre>
2520<p>If <code>value</code> is not a string, number, or <code>Buffer</code>, this method will throw a
2521<code>TypeError</code>. If <code>value</code> is a number, it will be coerced to a valid byte value,
2522an integer between 0 and 255.</p>
2523<p>If <code>byteOffset</code> is not a number, it will be coerced to a number. Any arguments
2524that coerce to <code>NaN</code>, like <code>{}</code> or <code>undefined</code>, will search the whole buffer.
2525This behavior matches <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf"><code>String.prototype.lastIndexOf()</code></a>.</p>
2526
2527<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2528
2529<span class="hljs-keyword">const</span> b = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'abcdef'</span>);
2530
2531<span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span>
2532<span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span>
2533<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-number">99.9</span>));
2534<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-number">256</span> + <span class="hljs-number">99</span>));
2535
2536<span class="hljs-comment">// Passing a byteOffset that coerces to NaN.</span>
2537<span class="hljs-comment">// Prints: 1, searching the whole buffer.</span>
2538<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'b'</span>, <span class="hljs-literal">undefined</span>));
2539<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'b'</span>, {}));
2540
2541<span class="hljs-comment">// Passing a byteOffset that coerces to 0.</span>
2542<span class="hljs-comment">// Prints: -1, equivalent to passing 0.</span>
2543<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'b'</span>, <span class="hljs-literal">null</span>));
2544<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'b'</span>, []));</code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2545
2546<span class="hljs-keyword">const</span> b = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'abcdef'</span>);
2547
2548<span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span>
2549<span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span>
2550<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-number">99.9</span>));
2551<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-number">256</span> + <span class="hljs-number">99</span>));
2552
2553<span class="hljs-comment">// Passing a byteOffset that coerces to NaN.</span>
2554<span class="hljs-comment">// Prints: 1, searching the whole buffer.</span>
2555<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'b'</span>, <span class="hljs-literal">undefined</span>));
2556<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'b'</span>, {}));
2557
2558<span class="hljs-comment">// Passing a byteOffset that coerces to 0.</span>
2559<span class="hljs-comment">// Prints: -1, equivalent to passing 0.</span>
2560<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'b'</span>, <span class="hljs-literal">null</span>));
2561<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">lastIndexOf</span>(<span class="hljs-string">'b'</span>, []));</code><button class="copy-button">copy</button></pre>
2562<p>If <code>value</code> is an empty string or empty <code>Buffer</code>, <code>byteOffset</code> will be returned.</p>
2563<h4><code>buf.length</code><span><a class="mark" href="#buflength" id="buflength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_length"></a></h4>
2564<div class="api_metadata">
2565<span>Added in: v0.1.90</span>
2566</div>
2567<ul>
2568<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
2569</ul>
2570<p>Returns the number of bytes in <code>buf</code>.</p>
2571
2572<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2573
2574<span class="hljs-comment">// Create a `Buffer` and write a shorter string to it using UTF-8.</span>
2575
2576<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">1234</span>);
2577
2578<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>);
2579<span class="hljs-comment">// Prints: 1234</span>
2580
2581buf.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some string'</span>, <span class="hljs-number">0</span>, <span class="hljs-string">'utf8'</span>);
2582
2583<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>);
2584<span class="hljs-comment">// Prints: 1234</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2585
2586<span class="hljs-comment">// Create a `Buffer` and write a shorter string to it using UTF-8.</span>
2587
2588<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">1234</span>);
2589
2590<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>);
2591<span class="hljs-comment">// Prints: 1234</span>
2592
2593buf.<span class="hljs-title function_">write</span>(<span class="hljs-string">'some string'</span>, <span class="hljs-number">0</span>, <span class="hljs-string">'utf8'</span>);
2594
2595<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>);
2596<span class="hljs-comment">// Prints: 1234</span></code><button class="copy-button">copy</button></pre>
2597<h4><code>buf.parent</code><span><a class="mark" href="#bufparent" id="bufparent">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_parent"></a></h4>
2598<div class="api_metadata">
2599<span>Deprecated since: v8.0.0</span>
2600</div>
2601<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#bufbuffer"><code>buf.buffer</code></a> instead.</div><p></p>
2602<p>The <code>buf.parent</code> property is a deprecated alias for <code>buf.buffer</code>.</p>
2603<h4><code>buf.readBigInt64BE([offset])</code><span><a class="mark" href="#bufreadbigint64beoffset" id="bufreadbigint64beoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readbigint64be_offset"></a></h4>
2604<div class="api_metadata">
2605<span>Added in: v12.0.0, v10.20.0</span>
2606</div>
2607<ul>
2608<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to read. Must
2609satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
2610<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
2611</ul>
2612<p>Reads a signed, big-endian 64-bit integer from <code>buf</code> at the specified <code>offset</code>.</p>
2613<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed
2614values.</p>
2615<h4><code>buf.readBigInt64LE([offset])</code><span><a class="mark" href="#bufreadbigint64leoffset" id="bufreadbigint64leoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readbigint64le_offset"></a></h4>
2616<div class="api_metadata">
2617<span>Added in: v12.0.0, v10.20.0</span>
2618</div>
2619<ul>
2620<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to read. Must
2621satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
2622<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
2623</ul>
2624<p>Reads a signed, little-endian 64-bit integer from <code>buf</code> at the specified
2625<code>offset</code>.</p>
2626<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed
2627values.</p>
2628<h4><code>buf.readBigUInt64BE([offset])</code><span><a class="mark" href="#bufreadbiguint64beoffset" id="bufreadbiguint64beoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readbiguint64be_offset"></a></h4>
2629<div class="api_metadata">
2630<details class="changelog"><summary>History</summary>
2631<table>
2632<tbody><tr><th>Version</th><th>Changes</th></tr>
2633<tr><td>v14.10.0, v12.19.0</td>
2634<td><p>This function is also available as <code>buf.readBigUint64BE()</code>.</p></td></tr>
2635<tr><td>v12.0.0, v10.20.0</td>
2636<td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr>
2637</tbody></table>
2638</details>
2639</div>
2640<ul>
2641<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to read. Must
2642satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
2643<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
2644</ul>
2645<p>Reads an unsigned, big-endian 64-bit integer from <code>buf</code> at the specified
2646<code>offset</code>.</p>
2647<p>This function is also available under the <code>readBigUint64BE</code> alias.</p>
2648
2649<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2650
2651<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>]);
2652
2653<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readBigUInt64BE</span>(<span class="hljs-number">0</span>));
2654<span class="hljs-comment">// Prints: 4294967295n</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2655
2656<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>]);
2657
2658<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readBigUInt64BE</span>(<span class="hljs-number">0</span>));
2659<span class="hljs-comment">// Prints: 4294967295n</span></code><button class="copy-button">copy</button></pre>
2660<h4><code>buf.readBigUInt64LE([offset])</code><span><a class="mark" href="#bufreadbiguint64leoffset" id="bufreadbiguint64leoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readbiguint64le_offset"></a></h4>
2661<div class="api_metadata">
2662<details class="changelog"><summary>History</summary>
2663<table>
2664<tbody><tr><th>Version</th><th>Changes</th></tr>
2665<tr><td>v14.10.0, v12.19.0</td>
2666<td><p>This function is also available as <code>buf.readBigUint64LE()</code>.</p></td></tr>
2667<tr><td>v12.0.0, v10.20.0</td>
2668<td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr>
2669</tbody></table>
2670</details>
2671</div>
2672<ul>
2673<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to read. Must
2674satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
2675<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
2676</ul>
2677<p>Reads an unsigned, little-endian 64-bit integer from <code>buf</code> at the specified
2678<code>offset</code>.</p>
2679<p>This function is also available under the <code>readBigUint64LE</code> alias.</p>
2680
2681<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2682
2683<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>]);
2684
2685<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readBigUInt64LE</span>(<span class="hljs-number">0</span>));
2686<span class="hljs-comment">// Prints: 18446744069414584320n</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2687
2688<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0x00</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>, <span class="hljs-number">0xff</span>]);
2689
2690<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readBigUInt64LE</span>(<span class="hljs-number">0</span>));
2691<span class="hljs-comment">// Prints: 18446744069414584320n</span></code><button class="copy-button">copy</button></pre>
2692<h4><code>buf.readDoubleBE([offset])</code><span><a class="mark" href="#bufreaddoublebeoffset" id="bufreaddoublebeoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readdoublebe_offset"></a></h4>
2693<div class="api_metadata">
2694<details class="changelog"><summary>History</summary>
2695<table>
2696<tbody><tr><th>Version</th><th>Changes</th></tr>
2697<tr><td>v10.0.0</td>
2698<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2699<tr><td>v0.11.15</td>
2700<td><p><span>Added in: v0.11.15</span></p></td></tr>
2701</tbody></table>
2702</details>
2703</div>
2704<ul>
2705<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to read. Must
2706satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
2707<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
2708</ul>
2709<p>Reads a 64-bit, big-endian double from <code>buf</code> at the specified <code>offset</code>.</p>
2710
2711<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2712
2713<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>]);
2714
2715<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readDoubleBE</span>(<span class="hljs-number">0</span>));
2716<span class="hljs-comment">// Prints: 8.20788039913184e-304</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2717
2718<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>]);
2719
2720<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readDoubleBE</span>(<span class="hljs-number">0</span>));
2721<span class="hljs-comment">// Prints: 8.20788039913184e-304</span></code><button class="copy-button">copy</button></pre>
2722<h4><code>buf.readDoubleLE([offset])</code><span><a class="mark" href="#bufreaddoubleleoffset" id="bufreaddoubleleoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readdoublele_offset"></a></h4>
2723<div class="api_metadata">
2724<details class="changelog"><summary>History</summary>
2725<table>
2726<tbody><tr><th>Version</th><th>Changes</th></tr>
2727<tr><td>v10.0.0</td>
2728<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2729<tr><td>v0.11.15</td>
2730<td><p><span>Added in: v0.11.15</span></p></td></tr>
2731</tbody></table>
2732</details>
2733</div>
2734<ul>
2735<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to read. Must
2736satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
2737<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
2738</ul>
2739<p>Reads a 64-bit, little-endian double from <code>buf</code> at the specified <code>offset</code>.</p>
2740
2741<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2742
2743<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>]);
2744
2745<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readDoubleLE</span>(<span class="hljs-number">0</span>));
2746<span class="hljs-comment">// Prints: 5.447603722011605e-270</span>
2747<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readDoubleLE</span>(<span class="hljs-number">1</span>));
2748<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2749
2750<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>]);
2751
2752<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readDoubleLE</span>(<span class="hljs-number">0</span>));
2753<span class="hljs-comment">// Prints: 5.447603722011605e-270</span>
2754<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readDoubleLE</span>(<span class="hljs-number">1</span>));
2755<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre>
2756<h4><code>buf.readFloatBE([offset])</code><span><a class="mark" href="#bufreadfloatbeoffset" id="bufreadfloatbeoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readfloatbe_offset"></a></h4>
2757<div class="api_metadata">
2758<details class="changelog"><summary>History</summary>
2759<table>
2760<tbody><tr><th>Version</th><th>Changes</th></tr>
2761<tr><td>v10.0.0</td>
2762<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2763<tr><td>v0.11.15</td>
2764<td><p><span>Added in: v0.11.15</span></p></td></tr>
2765</tbody></table>
2766</details>
2767</div>
2768<ul>
2769<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to read. Must
2770satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
2771<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
2772</ul>
2773<p>Reads a 32-bit, big-endian float from <code>buf</code> at the specified <code>offset</code>.</p>
2774
2775<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2776
2777<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]);
2778
2779<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readFloatBE</span>(<span class="hljs-number">0</span>));
2780<span class="hljs-comment">// Prints: 2.387939260590663e-38</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2781
2782<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]);
2783
2784<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readFloatBE</span>(<span class="hljs-number">0</span>));
2785<span class="hljs-comment">// Prints: 2.387939260590663e-38</span></code><button class="copy-button">copy</button></pre>
2786<h4><code>buf.readFloatLE([offset])</code><span><a class="mark" href="#bufreadfloatleoffset" id="bufreadfloatleoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readfloatle_offset"></a></h4>
2787<div class="api_metadata">
2788<details class="changelog"><summary>History</summary>
2789<table>
2790<tbody><tr><th>Version</th><th>Changes</th></tr>
2791<tr><td>v10.0.0</td>
2792<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2793<tr><td>v0.11.15</td>
2794<td><p><span>Added in: v0.11.15</span></p></td></tr>
2795</tbody></table>
2796</details>
2797</div>
2798<ul>
2799<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to read. Must
2800satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
2801<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
2802</ul>
2803<p>Reads a 32-bit, little-endian float from <code>buf</code> at the specified <code>offset</code>.</p>
2804
2805<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2806
2807<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]);
2808
2809<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readFloatLE</span>(<span class="hljs-number">0</span>));
2810<span class="hljs-comment">// Prints: 1.539989614439558e-36</span>
2811<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readFloatLE</span>(<span class="hljs-number">1</span>));
2812<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2813
2814<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]);
2815
2816<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readFloatLE</span>(<span class="hljs-number">0</span>));
2817<span class="hljs-comment">// Prints: 1.539989614439558e-36</span>
2818<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readFloatLE</span>(<span class="hljs-number">1</span>));
2819<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre>
2820<h4><code>buf.readInt8([offset])</code><span><a class="mark" href="#bufreadint8offset" id="bufreadint8offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readint8_offset"></a></h4>
2821<div class="api_metadata">
2822<details class="changelog"><summary>History</summary>
2823<table>
2824<tbody><tr><th>Version</th><th>Changes</th></tr>
2825<tr><td>v10.0.0</td>
2826<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2827<tr><td>v0.5.0</td>
2828<td><p><span>Added in: v0.5.0</span></p></td></tr>
2829</tbody></table>
2830</details>
2831</div>
2832<ul>
2833<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to read. Must
2834satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li>
2835<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
2836</ul>
2837<p>Reads a signed 8-bit integer from <code>buf</code> at the specified <code>offset</code>.</p>
2838<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p>
2839
2840<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2841
2842<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([-<span class="hljs-number">1</span>, <span class="hljs-number">5</span>]);
2843
2844<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt8</span>(<span class="hljs-number">0</span>));
2845<span class="hljs-comment">// Prints: -1</span>
2846<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt8</span>(<span class="hljs-number">1</span>));
2847<span class="hljs-comment">// Prints: 5</span>
2848<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt8</span>(<span class="hljs-number">2</span>));
2849<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2850
2851<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([-<span class="hljs-number">1</span>, <span class="hljs-number">5</span>]);
2852
2853<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt8</span>(<span class="hljs-number">0</span>));
2854<span class="hljs-comment">// Prints: -1</span>
2855<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt8</span>(<span class="hljs-number">1</span>));
2856<span class="hljs-comment">// Prints: 5</span>
2857<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt8</span>(<span class="hljs-number">2</span>));
2858<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre>
2859<h4><code>buf.readInt16BE([offset])</code><span><a class="mark" href="#bufreadint16beoffset" id="bufreadint16beoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readint16be_offset"></a></h4>
2860<div class="api_metadata">
2861<details class="changelog"><summary>History</summary>
2862<table>
2863<tbody><tr><th>Version</th><th>Changes</th></tr>
2864<tr><td>v10.0.0</td>
2865<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2866<tr><td>v0.5.5</td>
2867<td><p><span>Added in: v0.5.5</span></p></td></tr>
2868</tbody></table>
2869</details>
2870</div>
2871<ul>
2872<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to read. Must
2873satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li>
2874<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
2875</ul>
2876<p>Reads a signed, big-endian 16-bit integer from <code>buf</code> at the specified <code>offset</code>.</p>
2877<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p>
2878
2879<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2880
2881<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0</span>, <span class="hljs-number">5</span>]);
2882
2883<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt16BE</span>(<span class="hljs-number">0</span>));
2884<span class="hljs-comment">// Prints: 5</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2885
2886<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0</span>, <span class="hljs-number">5</span>]);
2887
2888<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt16BE</span>(<span class="hljs-number">0</span>));
2889<span class="hljs-comment">// Prints: 5</span></code><button class="copy-button">copy</button></pre>
2890<h4><code>buf.readInt16LE([offset])</code><span><a class="mark" href="#bufreadint16leoffset" id="bufreadint16leoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readint16le_offset"></a></h4>
2891<div class="api_metadata">
2892<details class="changelog"><summary>History</summary>
2893<table>
2894<tbody><tr><th>Version</th><th>Changes</th></tr>
2895<tr><td>v10.0.0</td>
2896<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2897<tr><td>v0.5.5</td>
2898<td><p><span>Added in: v0.5.5</span></p></td></tr>
2899</tbody></table>
2900</details>
2901</div>
2902<ul>
2903<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to read. Must
2904satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li>
2905<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
2906</ul>
2907<p>Reads a signed, little-endian 16-bit integer from <code>buf</code> at the specified
2908<code>offset</code>.</p>
2909<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p>
2910
2911<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2912
2913<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0</span>, <span class="hljs-number">5</span>]);
2914
2915<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt16LE</span>(<span class="hljs-number">0</span>));
2916<span class="hljs-comment">// Prints: 1280</span>
2917<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt16LE</span>(<span class="hljs-number">1</span>));
2918<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2919
2920<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0</span>, <span class="hljs-number">5</span>]);
2921
2922<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt16LE</span>(<span class="hljs-number">0</span>));
2923<span class="hljs-comment">// Prints: 1280</span>
2924<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt16LE</span>(<span class="hljs-number">1</span>));
2925<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre>
2926<h4><code>buf.readInt32BE([offset])</code><span><a class="mark" href="#bufreadint32beoffset" id="bufreadint32beoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readint32be_offset"></a></h4>
2927<div class="api_metadata">
2928<details class="changelog"><summary>History</summary>
2929<table>
2930<tbody><tr><th>Version</th><th>Changes</th></tr>
2931<tr><td>v10.0.0</td>
2932<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2933<tr><td>v0.5.5</td>
2934<td><p><span>Added in: v0.5.5</span></p></td></tr>
2935</tbody></table>
2936</details>
2937</div>
2938<ul>
2939<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to read. Must
2940satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
2941<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
2942</ul>
2943<p>Reads a signed, big-endian 32-bit integer from <code>buf</code> at the specified <code>offset</code>.</p>
2944<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p>
2945
2946<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2947
2948<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">5</span>]);
2949
2950<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt32BE</span>(<span class="hljs-number">0</span>));
2951<span class="hljs-comment">// Prints: 5</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2952
2953<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">5</span>]);
2954
2955<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt32BE</span>(<span class="hljs-number">0</span>));
2956<span class="hljs-comment">// Prints: 5</span></code><button class="copy-button">copy</button></pre>
2957<h4><code>buf.readInt32LE([offset])</code><span><a class="mark" href="#bufreadint32leoffset" id="bufreadint32leoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readint32le_offset"></a></h4>
2958<div class="api_metadata">
2959<details class="changelog"><summary>History</summary>
2960<table>
2961<tbody><tr><th>Version</th><th>Changes</th></tr>
2962<tr><td>v10.0.0</td>
2963<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2964<tr><td>v0.5.5</td>
2965<td><p><span>Added in: v0.5.5</span></p></td></tr>
2966</tbody></table>
2967</details>
2968</div>
2969<ul>
2970<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to read. Must
2971satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
2972<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
2973</ul>
2974<p>Reads a signed, little-endian 32-bit integer from <code>buf</code> at the specified
2975<code>offset</code>.</p>
2976<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p>
2977
2978<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
2979
2980<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">5</span>]);
2981
2982<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt32LE</span>(<span class="hljs-number">0</span>));
2983<span class="hljs-comment">// Prints: 83886080</span>
2984<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt32LE</span>(<span class="hljs-number">1</span>));
2985<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
2986
2987<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">5</span>]);
2988
2989<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt32LE</span>(<span class="hljs-number">0</span>));
2990<span class="hljs-comment">// Prints: 83886080</span>
2991<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readInt32LE</span>(<span class="hljs-number">1</span>));
2992<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre>
2993<h4><code>buf.readIntBE(offset, byteLength)</code><span><a class="mark" href="#bufreadintbeoffset-bytelength" id="bufreadintbeoffset-bytelength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readintbe_offset_bytelength"></a></h4>
2994<div class="api_metadata">
2995<details class="changelog"><summary>History</summary>
2996<table>
2997<tbody><tr><th>Version</th><th>Changes</th></tr>
2998<tr><td>v10.0.0</td>
2999<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset and <code>byteLength</code> to <code>uint32</code> anymore.</p></td></tr>
3000<tr><td>v0.11.15</td>
3001<td><p><span>Added in: v0.11.15</span></p></td></tr>
3002</tbody></table>
3003</details>
3004</div>
3005<ul>
3006<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to read. Must
3007satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
3008<li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to read. Must satisfy
3009<code>0 &#x3C; byteLength &#x3C;= 6</code>.</li>
3010<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
3011</ul>
3012<p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code>
3013and interprets the result as a big-endian, two's complement signed value
3014supporting up to 48 bits of accuracy.</p>
3015
3016<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3017
3018<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>, <span class="hljs-number">0x90</span>, <span class="hljs-number">0xab</span>]);
3019
3020<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readIntBE</span>(<span class="hljs-number">0</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3021<span class="hljs-comment">// Prints: 1234567890ab</span>
3022<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readIntBE</span>(<span class="hljs-number">1</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3023<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span>
3024<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readIntBE</span>(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3025<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3026
3027<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>, <span class="hljs-number">0x90</span>, <span class="hljs-number">0xab</span>]);
3028
3029<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readIntBE</span>(<span class="hljs-number">0</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3030<span class="hljs-comment">// Prints: 1234567890ab</span>
3031<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readIntBE</span>(<span class="hljs-number">1</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3032<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span>
3033<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readIntBE</span>(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3034<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre>
3035<h4><code>buf.readIntLE(offset, byteLength)</code><span><a class="mark" href="#bufreadintleoffset-bytelength" id="bufreadintleoffset-bytelength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readintle_offset_bytelength"></a></h4>
3036<div class="api_metadata">
3037<details class="changelog"><summary>History</summary>
3038<table>
3039<tbody><tr><th>Version</th><th>Changes</th></tr>
3040<tr><td>v10.0.0</td>
3041<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset and <code>byteLength</code> to <code>uint32</code> anymore.</p></td></tr>
3042<tr><td>v0.11.15</td>
3043<td><p><span>Added in: v0.11.15</span></p></td></tr>
3044</tbody></table>
3045</details>
3046</div>
3047<ul>
3048<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to read. Must
3049satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
3050<li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to read. Must satisfy
3051<code>0 &#x3C; byteLength &#x3C;= 6</code>.</li>
3052<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
3053</ul>
3054<p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code>
3055and interprets the result as a little-endian, two's complement signed value
3056supporting up to 48 bits of accuracy.</p>
3057
3058<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3059
3060<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>, <span class="hljs-number">0x90</span>, <span class="hljs-number">0xab</span>]);
3061
3062<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readIntLE</span>(<span class="hljs-number">0</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3063<span class="hljs-comment">// Prints: -546f87a9cbee</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3064
3065<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>, <span class="hljs-number">0x90</span>, <span class="hljs-number">0xab</span>]);
3066
3067<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readIntLE</span>(<span class="hljs-number">0</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3068<span class="hljs-comment">// Prints: -546f87a9cbee</span></code><button class="copy-button">copy</button></pre>
3069<h4><code>buf.readUInt8([offset])</code><span><a class="mark" href="#bufreaduint8offset" id="bufreaduint8offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readuint8_offset"></a></h4>
3070<div class="api_metadata">
3071<details class="changelog"><summary>History</summary>
3072<table>
3073<tbody><tr><th>Version</th><th>Changes</th></tr>
3074<tr><td>v14.9.0, v12.19.0</td>
3075<td><p>This function is also available as <code>buf.readUint8()</code>.</p></td></tr>
3076<tr><td>v10.0.0</td>
3077<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
3078<tr><td>v0.5.0</td>
3079<td><p><span>Added in: v0.5.0</span></p></td></tr>
3080</tbody></table>
3081</details>
3082</div>
3083<ul>
3084<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to read. Must
3085satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li>
3086<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
3087</ul>
3088<p>Reads an unsigned 8-bit integer from <code>buf</code> at the specified <code>offset</code>.</p>
3089<p>This function is also available under the <code>readUint8</code> alias.</p>
3090
3091<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3092
3093<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, -<span class="hljs-number">2</span>]);
3094
3095<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt8</span>(<span class="hljs-number">0</span>));
3096<span class="hljs-comment">// Prints: 1</span>
3097<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt8</span>(<span class="hljs-number">1</span>));
3098<span class="hljs-comment">// Prints: 254</span>
3099<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt8</span>(<span class="hljs-number">2</span>));
3100<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3101
3102<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">1</span>, -<span class="hljs-number">2</span>]);
3103
3104<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt8</span>(<span class="hljs-number">0</span>));
3105<span class="hljs-comment">// Prints: 1</span>
3106<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt8</span>(<span class="hljs-number">1</span>));
3107<span class="hljs-comment">// Prints: 254</span>
3108<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt8</span>(<span class="hljs-number">2</span>));
3109<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre>
3110<h4><code>buf.readUInt16BE([offset])</code><span><a class="mark" href="#bufreaduint16beoffset" id="bufreaduint16beoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readuint16be_offset"></a></h4>
3111<div class="api_metadata">
3112<details class="changelog"><summary>History</summary>
3113<table>
3114<tbody><tr><th>Version</th><th>Changes</th></tr>
3115<tr><td>v14.9.0, v12.19.0</td>
3116<td><p>This function is also available as <code>buf.readUint16BE()</code>.</p></td></tr>
3117<tr><td>v10.0.0</td>
3118<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
3119<tr><td>v0.5.5</td>
3120<td><p><span>Added in: v0.5.5</span></p></td></tr>
3121</tbody></table>
3122</details>
3123</div>
3124<ul>
3125<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to read. Must
3126satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li>
3127<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
3128</ul>
3129<p>Reads an unsigned, big-endian 16-bit integer from <code>buf</code> at the specified
3130<code>offset</code>.</p>
3131<p>This function is also available under the <code>readUint16BE</code> alias.</p>
3132
3133<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3134
3135<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>]);
3136
3137<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt16BE</span>(<span class="hljs-number">0</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3138<span class="hljs-comment">// Prints: 1234</span>
3139<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt16BE</span>(<span class="hljs-number">1</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3140<span class="hljs-comment">// Prints: 3456</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3141
3142<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>]);
3143
3144<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt16BE</span>(<span class="hljs-number">0</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3145<span class="hljs-comment">// Prints: 1234</span>
3146<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt16BE</span>(<span class="hljs-number">1</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3147<span class="hljs-comment">// Prints: 3456</span></code><button class="copy-button">copy</button></pre>
3148<h4><code>buf.readUInt16LE([offset])</code><span><a class="mark" href="#bufreaduint16leoffset" id="bufreaduint16leoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readuint16le_offset"></a></h4>
3149<div class="api_metadata">
3150<details class="changelog"><summary>History</summary>
3151<table>
3152<tbody><tr><th>Version</th><th>Changes</th></tr>
3153<tr><td>v14.9.0, v12.19.0</td>
3154<td><p>This function is also available as <code>buf.readUint16LE()</code>.</p></td></tr>
3155<tr><td>v10.0.0</td>
3156<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
3157<tr><td>v0.5.5</td>
3158<td><p><span>Added in: v0.5.5</span></p></td></tr>
3159</tbody></table>
3160</details>
3161</div>
3162<ul>
3163<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to read. Must
3164satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li>
3165<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
3166</ul>
3167<p>Reads an unsigned, little-endian 16-bit integer from <code>buf</code> at the specified
3168<code>offset</code>.</p>
3169<p>This function is also available under the <code>readUint16LE</code> alias.</p>
3170
3171<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3172
3173<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>]);
3174
3175<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt16LE</span>(<span class="hljs-number">0</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3176<span class="hljs-comment">// Prints: 3412</span>
3177<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt16LE</span>(<span class="hljs-number">1</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3178<span class="hljs-comment">// Prints: 5634</span>
3179<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt16LE</span>(<span class="hljs-number">2</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3180<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3181
3182<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>]);
3183
3184<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt16LE</span>(<span class="hljs-number">0</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3185<span class="hljs-comment">// Prints: 3412</span>
3186<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt16LE</span>(<span class="hljs-number">1</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3187<span class="hljs-comment">// Prints: 5634</span>
3188<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt16LE</span>(<span class="hljs-number">2</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3189<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre>
3190<h4><code>buf.readUInt32BE([offset])</code><span><a class="mark" href="#bufreaduint32beoffset" id="bufreaduint32beoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readuint32be_offset"></a></h4>
3191<div class="api_metadata">
3192<details class="changelog"><summary>History</summary>
3193<table>
3194<tbody><tr><th>Version</th><th>Changes</th></tr>
3195<tr><td>v14.9.0, v12.19.0</td>
3196<td><p>This function is also available as <code>buf.readUint32BE()</code>.</p></td></tr>
3197<tr><td>v10.0.0</td>
3198<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
3199<tr><td>v0.5.5</td>
3200<td><p><span>Added in: v0.5.5</span></p></td></tr>
3201</tbody></table>
3202</details>
3203</div>
3204<ul>
3205<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to read. Must
3206satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
3207<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
3208</ul>
3209<p>Reads an unsigned, big-endian 32-bit integer from <code>buf</code> at the specified
3210<code>offset</code>.</p>
3211<p>This function is also available under the <code>readUint32BE</code> alias.</p>
3212
3213<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3214
3215<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>]);
3216
3217<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt32BE</span>(<span class="hljs-number">0</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3218<span class="hljs-comment">// Prints: 12345678</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3219
3220<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>]);
3221
3222<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt32BE</span>(<span class="hljs-number">0</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3223<span class="hljs-comment">// Prints: 12345678</span></code><button class="copy-button">copy</button></pre>
3224<h4><code>buf.readUInt32LE([offset])</code><span><a class="mark" href="#bufreaduint32leoffset" id="bufreaduint32leoffset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readuint32le_offset"></a></h4>
3225<div class="api_metadata">
3226<details class="changelog"><summary>History</summary>
3227<table>
3228<tbody><tr><th>Version</th><th>Changes</th></tr>
3229<tr><td>v14.9.0, v12.19.0</td>
3230<td><p>This function is also available as <code>buf.readUint32LE()</code>.</p></td></tr>
3231<tr><td>v10.0.0</td>
3232<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
3233<tr><td>v0.5.5</td>
3234<td><p><span>Added in: v0.5.5</span></p></td></tr>
3235</tbody></table>
3236</details>
3237</div>
3238<ul>
3239<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to read. Must
3240satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
3241<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
3242</ul>
3243<p>Reads an unsigned, little-endian 32-bit integer from <code>buf</code> at the specified
3244<code>offset</code>.</p>
3245<p>This function is also available under the <code>readUint32LE</code> alias.</p>
3246
3247<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3248
3249<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>]);
3250
3251<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt32LE</span>(<span class="hljs-number">0</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3252<span class="hljs-comment">// Prints: 78563412</span>
3253<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt32LE</span>(<span class="hljs-number">1</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3254<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3255
3256<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>]);
3257
3258<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt32LE</span>(<span class="hljs-number">0</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3259<span class="hljs-comment">// Prints: 78563412</span>
3260<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUInt32LE</span>(<span class="hljs-number">1</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3261<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre>
3262<h4><code>buf.readUIntBE(offset, byteLength)</code><span><a class="mark" href="#bufreaduintbeoffset-bytelength" id="bufreaduintbeoffset-bytelength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readuintbe_offset_bytelength"></a></h4>
3263<div class="api_metadata">
3264<details class="changelog"><summary>History</summary>
3265<table>
3266<tbody><tr><th>Version</th><th>Changes</th></tr>
3267<tr><td>v14.9.0, v12.19.0</td>
3268<td><p>This function is also available as <code>buf.readUintBE()</code>.</p></td></tr>
3269<tr><td>v10.0.0</td>
3270<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset and <code>byteLength</code> to <code>uint32</code> anymore.</p></td></tr>
3271<tr><td>v0.11.15</td>
3272<td><p><span>Added in: v0.11.15</span></p></td></tr>
3273</tbody></table>
3274</details>
3275</div>
3276<ul>
3277<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to read. Must
3278satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
3279<li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to read. Must satisfy
3280<code>0 &#x3C; byteLength &#x3C;= 6</code>.</li>
3281<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
3282</ul>
3283<p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code>
3284and interprets the result as an unsigned big-endian integer supporting
3285up to 48 bits of accuracy.</p>
3286<p>This function is also available under the <code>readUintBE</code> alias.</p>
3287
3288<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3289
3290<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>, <span class="hljs-number">0x90</span>, <span class="hljs-number">0xab</span>]);
3291
3292<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUIntBE</span>(<span class="hljs-number">0</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3293<span class="hljs-comment">// Prints: 1234567890ab</span>
3294<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUIntBE</span>(<span class="hljs-number">1</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3295<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3296
3297<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>, <span class="hljs-number">0x90</span>, <span class="hljs-number">0xab</span>]);
3298
3299<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUIntBE</span>(<span class="hljs-number">0</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3300<span class="hljs-comment">// Prints: 1234567890ab</span>
3301<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUIntBE</span>(<span class="hljs-number">1</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3302<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code><button class="copy-button">copy</button></pre>
3303<h4><code>buf.readUIntLE(offset, byteLength)</code><span><a class="mark" href="#bufreaduintleoffset-bytelength" id="bufreaduintleoffset-bytelength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_readuintle_offset_bytelength"></a></h4>
3304<div class="api_metadata">
3305<details class="changelog"><summary>History</summary>
3306<table>
3307<tbody><tr><th>Version</th><th>Changes</th></tr>
3308<tr><td>v14.9.0, v12.19.0</td>
3309<td><p>This function is also available as <code>buf.readUintLE()</code>.</p></td></tr>
3310<tr><td>v10.0.0</td>
3311<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset and <code>byteLength</code> to <code>uint32</code> anymore.</p></td></tr>
3312<tr><td>v0.11.15</td>
3313<td><p><span>Added in: v0.11.15</span></p></td></tr>
3314</tbody></table>
3315</details>
3316</div>
3317<ul>
3318<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to read. Must
3319satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
3320<li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to read. Must satisfy
3321<code>0 &#x3C; byteLength &#x3C;= 6</code>.</li>
3322<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
3323</ul>
3324<p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code>
3325and interprets the result as an unsigned, little-endian integer supporting
3326up to 48 bits of accuracy.</p>
3327<p>This function is also available under the <code>readUintLE</code> alias.</p>
3328
3329<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3330
3331<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>, <span class="hljs-number">0x90</span>, <span class="hljs-number">0xab</span>]);
3332
3333<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUIntLE</span>(<span class="hljs-number">0</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3334<span class="hljs-comment">// Prints: ab9078563412</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3335
3336<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x12</span>, <span class="hljs-number">0x34</span>, <span class="hljs-number">0x56</span>, <span class="hljs-number">0x78</span>, <span class="hljs-number">0x90</span>, <span class="hljs-number">0xab</span>]);
3337
3338<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">readUIntLE</span>(<span class="hljs-number">0</span>, <span class="hljs-number">6</span>).<span class="hljs-title function_">toString</span>(<span class="hljs-number">16</span>));
3339<span class="hljs-comment">// Prints: ab9078563412</span></code><button class="copy-button">copy</button></pre>
3340<h4><code>buf.subarray([start[, end]])</code><span><a class="mark" href="#bufsubarraystart-end" id="bufsubarraystart-end">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_subarray_start_end"></a></h4>
3341<div class="api_metadata">
3342<span>Added in: v3.0.0</span>
3343</div>
3344<ul>
3345<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Where the new <code>Buffer</code> will start. <strong>Default:</strong> <code>0</code>.</li>
3346<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Where the new <code>Buffer</code> will end (not inclusive).
3347<strong>Default:</strong> <a href="#buflength"><code>buf.length</code></a>.</li>
3348<li>Returns: <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
3349</ul>
3350<p>Returns a new <code>Buffer</code> that references the same memory as the original, but
3351offset and cropped by the <code>start</code> and <code>end</code> indices.</p>
3352<p>Specifying <code>end</code> greater than <a href="#buflength"><code>buf.length</code></a> will return the same result as
3353that of <code>end</code> equal to <a href="#buflength"><code>buf.length</code></a>.</p>
3354<p>This method is inherited from <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray"><code>TypedArray.prototype.subarray()</code></a>.</p>
3355<p>Modifying the new <code>Buffer</code> slice will modify the memory in the original <code>Buffer</code>
3356because the allocated memory of the two objects overlap.</p>
3357
3358<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3359
3360<span class="hljs-comment">// Create a `Buffer` with the ASCII alphabet, take a slice, and modify one byte</span>
3361<span class="hljs-comment">// from the original `Buffer`.</span>
3362
3363<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">26</span>);
3364
3365<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &#x3C; <span class="hljs-number">26</span>; i++) {
3366  <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
3367  buf1[i] = i + <span class="hljs-number">97</span>;
3368}
3369
3370<span class="hljs-keyword">const</span> buf2 = buf1.<span class="hljs-title function_">subarray</span>(<span class="hljs-number">0</span>, <span class="hljs-number">3</span>);
3371
3372<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'ascii'</span>, <span class="hljs-number">0</span>, buf2.<span class="hljs-property">length</span>));
3373<span class="hljs-comment">// Prints: abc</span>
3374
3375buf1[<span class="hljs-number">0</span>] = <span class="hljs-number">33</span>;
3376
3377<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'ascii'</span>, <span class="hljs-number">0</span>, buf2.<span class="hljs-property">length</span>));
3378<span class="hljs-comment">// Prints: !bc</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3379
3380<span class="hljs-comment">// Create a `Buffer` with the ASCII alphabet, take a slice, and modify one byte</span>
3381<span class="hljs-comment">// from the original `Buffer`.</span>
3382
3383<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">26</span>);
3384
3385<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &#x3C; <span class="hljs-number">26</span>; i++) {
3386  <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
3387  buf1[i] = i + <span class="hljs-number">97</span>;
3388}
3389
3390<span class="hljs-keyword">const</span> buf2 = buf1.<span class="hljs-title function_">subarray</span>(<span class="hljs-number">0</span>, <span class="hljs-number">3</span>);
3391
3392<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'ascii'</span>, <span class="hljs-number">0</span>, buf2.<span class="hljs-property">length</span>));
3393<span class="hljs-comment">// Prints: abc</span>
3394
3395buf1[<span class="hljs-number">0</span>] = <span class="hljs-number">33</span>;
3396
3397<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'ascii'</span>, <span class="hljs-number">0</span>, buf2.<span class="hljs-property">length</span>));
3398<span class="hljs-comment">// Prints: !bc</span></code><button class="copy-button">copy</button></pre>
3399<p>Specifying negative indexes causes the slice to be generated relative to the
3400end of <code>buf</code> rather than the beginning.</p>
3401
3402<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3403
3404<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>);
3405
3406<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">subarray</span>(-<span class="hljs-number">6</span>, -<span class="hljs-number">1</span>).<span class="hljs-title function_">toString</span>());
3407<span class="hljs-comment">// Prints: buffe</span>
3408<span class="hljs-comment">// (Equivalent to buf.subarray(0, 5).)</span>
3409
3410<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">subarray</span>(-<span class="hljs-number">6</span>, -<span class="hljs-number">2</span>).<span class="hljs-title function_">toString</span>());
3411<span class="hljs-comment">// Prints: buff</span>
3412<span class="hljs-comment">// (Equivalent to buf.subarray(0, 4).)</span>
3413
3414<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">subarray</span>(-<span class="hljs-number">5</span>, -<span class="hljs-number">2</span>).<span class="hljs-title function_">toString</span>());
3415<span class="hljs-comment">// Prints: uff</span>
3416<span class="hljs-comment">// (Equivalent to buf.subarray(1, 4).)</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3417
3418<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>);
3419
3420<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">subarray</span>(-<span class="hljs-number">6</span>, -<span class="hljs-number">1</span>).<span class="hljs-title function_">toString</span>());
3421<span class="hljs-comment">// Prints: buffe</span>
3422<span class="hljs-comment">// (Equivalent to buf.subarray(0, 5).)</span>
3423
3424<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">subarray</span>(-<span class="hljs-number">6</span>, -<span class="hljs-number">2</span>).<span class="hljs-title function_">toString</span>());
3425<span class="hljs-comment">// Prints: buff</span>
3426<span class="hljs-comment">// (Equivalent to buf.subarray(0, 4).)</span>
3427
3428<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">subarray</span>(-<span class="hljs-number">5</span>, -<span class="hljs-number">2</span>).<span class="hljs-title function_">toString</span>());
3429<span class="hljs-comment">// Prints: uff</span>
3430<span class="hljs-comment">// (Equivalent to buf.subarray(1, 4).)</span></code><button class="copy-button">copy</button></pre>
3431<h4><code>buf.slice([start[, end]])</code><span><a class="mark" href="#bufslicestart-end" id="bufslicestart-end">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_slice_start_end"></a></h4>
3432<div class="api_metadata">
3433<details class="changelog"><summary>History</summary>
3434<table>
3435<tbody><tr><th>Version</th><th>Changes</th></tr>
3436<tr><td>v17.5.0</td>
3437<td><p>The buf.slice() method has been deprecated.</p></td></tr>
3438<tr><td>v7.0.0</td>
3439<td><p>All offsets are now coerced to integers before doing any calculations with them.</p></td></tr>
3440<tr><td>v7.1.0, v6.9.2</td>
3441<td><p>Coercing the offsets to integers now handles values outside the 32-bit integer range properly.</p></td></tr>
3442<tr><td>v0.3.0</td>
3443<td><p><span>Added in: v0.3.0</span></p></td></tr>
3444</tbody></table>
3445</details>
3446</div>
3447<ul>
3448<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Where the new <code>Buffer</code> will start. <strong>Default:</strong> <code>0</code>.</li>
3449<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Where the new <code>Buffer</code> will end (not inclusive).
3450<strong>Default:</strong> <a href="#buflength"><code>buf.length</code></a>.</li>
3451<li>Returns: <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
3452</ul>
3453<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#bufsubarraystart-end"><code>buf.subarray</code></a> instead.</div><p></p>
3454<p>Returns a new <code>Buffer</code> that references the same memory as the original, but
3455offset and cropped by the <code>start</code> and <code>end</code> indices.</p>
3456<p>This method is not compatible with the <code>Uint8Array.prototype.slice()</code>,
3457which is a superclass of <code>Buffer</code>. To copy the slice, use
3458<code>Uint8Array.prototype.slice()</code>.</p>
3459
3460<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3461
3462<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>);
3463
3464<span class="hljs-keyword">const</span> copiedBuf = <span class="hljs-title class_">Uint8Array</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">slice</span>.<span class="hljs-title function_">call</span>(buf);
3465copiedBuf[<span class="hljs-number">0</span>]++;
3466<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(copiedBuf.<span class="hljs-title function_">toString</span>());
3467<span class="hljs-comment">// Prints: cuffer</span>
3468
3469<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>());
3470<span class="hljs-comment">// Prints: buffer</span>
3471
3472<span class="hljs-comment">// With buf.slice(), the original buffer is modified.</span>
3473<span class="hljs-keyword">const</span> notReallyCopiedBuf = buf.<span class="hljs-title function_">slice</span>();
3474notReallyCopiedBuf[<span class="hljs-number">0</span>]++;
3475<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(notReallyCopiedBuf.<span class="hljs-title function_">toString</span>());
3476<span class="hljs-comment">// Prints: cuffer</span>
3477<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>());
3478<span class="hljs-comment">// Also prints: cuffer (!)</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3479
3480<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>);
3481
3482<span class="hljs-keyword">const</span> copiedBuf = <span class="hljs-title class_">Uint8Array</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">slice</span>.<span class="hljs-title function_">call</span>(buf);
3483copiedBuf[<span class="hljs-number">0</span>]++;
3484<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(copiedBuf.<span class="hljs-title function_">toString</span>());
3485<span class="hljs-comment">// Prints: cuffer</span>
3486
3487<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>());
3488<span class="hljs-comment">// Prints: buffer</span>
3489
3490<span class="hljs-comment">// With buf.slice(), the original buffer is modified.</span>
3491<span class="hljs-keyword">const</span> notReallyCopiedBuf = buf.<span class="hljs-title function_">slice</span>();
3492notReallyCopiedBuf[<span class="hljs-number">0</span>]++;
3493<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(notReallyCopiedBuf.<span class="hljs-title function_">toString</span>());
3494<span class="hljs-comment">// Prints: cuffer</span>
3495<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>());
3496<span class="hljs-comment">// Also prints: cuffer (!)</span></code><button class="copy-button">copy</button></pre>
3497<h4><code>buf.swap16()</code><span><a class="mark" href="#bufswap16" id="bufswap16">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_swap16"></a></h4>
3498<div class="api_metadata">
3499<span>Added in: v5.10.0</span>
3500</div>
3501<ul>
3502<li>Returns: <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> A reference to <code>buf</code>.</li>
3503</ul>
3504<p>Interprets <code>buf</code> as an array of unsigned 16-bit integers and swaps the
3505byte order <em>in-place</em>. Throws <a href="errors.html#err_invalid_buffer_size"><code>ERR_INVALID_BUFFER_SIZE</code></a> if <a href="#buflength"><code>buf.length</code></a>
3506is not a multiple of 2.</p>
3507
3508<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3509
3510<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>, <span class="hljs-number">0x4</span>, <span class="hljs-number">0x5</span>, <span class="hljs-number">0x6</span>, <span class="hljs-number">0x7</span>, <span class="hljs-number">0x8</span>]);
3511
3512<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
3513<span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span>
3514
3515buf1.<span class="hljs-title function_">swap16</span>();
3516
3517<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
3518<span class="hljs-comment">// Prints: &#x3C;Buffer 02 01 04 03 06 05 08 07></span>
3519
3520<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>]);
3521
3522buf2.<span class="hljs-title function_">swap16</span>();
3523<span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3524
3525<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>, <span class="hljs-number">0x4</span>, <span class="hljs-number">0x5</span>, <span class="hljs-number">0x6</span>, <span class="hljs-number">0x7</span>, <span class="hljs-number">0x8</span>]);
3526
3527<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
3528<span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span>
3529
3530buf1.<span class="hljs-title function_">swap16</span>();
3531
3532<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
3533<span class="hljs-comment">// Prints: &#x3C;Buffer 02 01 04 03 06 05 08 07></span>
3534
3535<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>]);
3536
3537buf2.<span class="hljs-title function_">swap16</span>();
3538<span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code><button class="copy-button">copy</button></pre>
3539<p>One convenient use of <code>buf.swap16()</code> is to perform a fast in-place conversion
3540between UTF-16 little-endian and UTF-16 big-endian:</p>
3541
3542<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3543
3544<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'This is little-endian UTF-16'</span>, <span class="hljs-string">'utf16le'</span>);
3545buf.<span class="hljs-title function_">swap16</span>(); <span class="hljs-comment">// Convert to big-endian UTF-16 text.</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3546
3547<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'This is little-endian UTF-16'</span>, <span class="hljs-string">'utf16le'</span>);
3548buf.<span class="hljs-title function_">swap16</span>(); <span class="hljs-comment">// Convert to big-endian UTF-16 text.</span></code><button class="copy-button">copy</button></pre>
3549<h4><code>buf.swap32()</code><span><a class="mark" href="#bufswap32" id="bufswap32">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_swap32"></a></h4>
3550<div class="api_metadata">
3551<span>Added in: v5.10.0</span>
3552</div>
3553<ul>
3554<li>Returns: <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> A reference to <code>buf</code>.</li>
3555</ul>
3556<p>Interprets <code>buf</code> as an array of unsigned 32-bit integers and swaps the
3557byte order <em>in-place</em>. Throws <a href="errors.html#err_invalid_buffer_size"><code>ERR_INVALID_BUFFER_SIZE</code></a> if <a href="#buflength"><code>buf.length</code></a>
3558is not a multiple of 4.</p>
3559
3560<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3561
3562<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>, <span class="hljs-number">0x4</span>, <span class="hljs-number">0x5</span>, <span class="hljs-number">0x6</span>, <span class="hljs-number">0x7</span>, <span class="hljs-number">0x8</span>]);
3563
3564<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
3565<span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span>
3566
3567buf1.<span class="hljs-title function_">swap32</span>();
3568
3569<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
3570<span class="hljs-comment">// Prints: &#x3C;Buffer 04 03 02 01 08 07 06 05></span>
3571
3572<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>]);
3573
3574buf2.<span class="hljs-title function_">swap32</span>();
3575<span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3576
3577<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>, <span class="hljs-number">0x4</span>, <span class="hljs-number">0x5</span>, <span class="hljs-number">0x6</span>, <span class="hljs-number">0x7</span>, <span class="hljs-number">0x8</span>]);
3578
3579<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
3580<span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span>
3581
3582buf1.<span class="hljs-title function_">swap32</span>();
3583
3584<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
3585<span class="hljs-comment">// Prints: &#x3C;Buffer 04 03 02 01 08 07 06 05></span>
3586
3587<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>]);
3588
3589buf2.<span class="hljs-title function_">swap32</span>();
3590<span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code><button class="copy-button">copy</button></pre>
3591<h4><code>buf.swap64()</code><span><a class="mark" href="#bufswap64" id="bufswap64">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_swap64"></a></h4>
3592<div class="api_metadata">
3593<span>Added in: v6.3.0</span>
3594</div>
3595<ul>
3596<li>Returns: <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> A reference to <code>buf</code>.</li>
3597</ul>
3598<p>Interprets <code>buf</code> as an array of 64-bit numbers and swaps byte order <em>in-place</em>.
3599Throws <a href="errors.html#err_invalid_buffer_size"><code>ERR_INVALID_BUFFER_SIZE</code></a> if <a href="#buflength"><code>buf.length</code></a> is not a multiple of 8.</p>
3600
3601<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3602
3603<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>, <span class="hljs-number">0x4</span>, <span class="hljs-number">0x5</span>, <span class="hljs-number">0x6</span>, <span class="hljs-number">0x7</span>, <span class="hljs-number">0x8</span>]);
3604
3605<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
3606<span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span>
3607
3608buf1.<span class="hljs-title function_">swap64</span>();
3609
3610<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
3611<span class="hljs-comment">// Prints: &#x3C;Buffer 08 07 06 05 04 03 02 01></span>
3612
3613<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>]);
3614
3615buf2.<span class="hljs-title function_">swap64</span>();
3616<span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3617
3618<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>, <span class="hljs-number">0x4</span>, <span class="hljs-number">0x5</span>, <span class="hljs-number">0x6</span>, <span class="hljs-number">0x7</span>, <span class="hljs-number">0x8</span>]);
3619
3620<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
3621<span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span>
3622
3623buf1.<span class="hljs-title function_">swap64</span>();
3624
3625<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
3626<span class="hljs-comment">// Prints: &#x3C;Buffer 08 07 06 05 04 03 02 01></span>
3627
3628<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>]);
3629
3630buf2.<span class="hljs-title function_">swap64</span>();
3631<span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code><button class="copy-button">copy</button></pre>
3632<h4><code>buf.toJSON()</code><span><a class="mark" href="#buftojson" id="buftojson">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_tojson"></a></h4>
3633<div class="api_metadata">
3634<span>Added in: v0.9.2</span>
3635</div>
3636<ul>
3637<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
3638</ul>
3639<p>Returns a JSON representation of <code>buf</code>. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify"><code>JSON.stringify()</code></a> implicitly calls
3640this function when stringifying a <code>Buffer</code> instance.</p>
3641<p><code>Buffer.from()</code> accepts objects in the format returned from this method.
3642In particular, <code>Buffer.from(buf.toJSON())</code> works like <code>Buffer.from(buf)</code>.</p>
3643
3644<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3645
3646<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>, <span class="hljs-number">0x4</span>, <span class="hljs-number">0x5</span>]);
3647<span class="hljs-keyword">const</span> json = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(buf);
3648
3649<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(json);
3650<span class="hljs-comment">// Prints: {"type":"Buffer","data":[1,2,3,4,5]}</span>
3651
3652<span class="hljs-keyword">const</span> copy = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">parse</span>(json, <span class="hljs-function">(<span class="hljs-params">key, value</span>) =></span> {
3653  <span class="hljs-keyword">return</span> value &#x26;&#x26; value.<span class="hljs-property">type</span> === <span class="hljs-string">'Buffer'</span> ?
3654    <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(value) :
3655    value;
3656});
3657
3658<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(copy);
3659<span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3660
3661<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>([<span class="hljs-number">0x1</span>, <span class="hljs-number">0x2</span>, <span class="hljs-number">0x3</span>, <span class="hljs-number">0x4</span>, <span class="hljs-number">0x5</span>]);
3662<span class="hljs-keyword">const</span> json = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(buf);
3663
3664<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(json);
3665<span class="hljs-comment">// Prints: {"type":"Buffer","data":[1,2,3,4,5]}</span>
3666
3667<span class="hljs-keyword">const</span> copy = <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">parse</span>(json, <span class="hljs-function">(<span class="hljs-params">key, value</span>) =></span> {
3668  <span class="hljs-keyword">return</span> value &#x26;&#x26; value.<span class="hljs-property">type</span> === <span class="hljs-string">'Buffer'</span> ?
3669    <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(value) :
3670    value;
3671});
3672
3673<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(copy);
3674<span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05></span></code><button class="copy-button">copy</button></pre>
3675<h4><code>buf.toString([encoding[, start[, end]]])</code><span><a class="mark" href="#buftostringencoding-start-end" id="buftostringencoding-start-end">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_tostring_encoding_start_end"></a></h4>
3676<div class="api_metadata">
3677<span>Added in: v0.1.90</span>
3678</div>
3679<ul>
3680<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The character encoding to use. <strong>Default:</strong> <code>'utf8'</code>.</li>
3681<li><code>start</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The byte offset to start decoding at. <strong>Default:</strong> <code>0</code>.</li>
3682<li><code>end</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The byte offset to stop decoding at (not inclusive).
3683<strong>Default:</strong> <a href="#buflength"><code>buf.length</code></a>.</li>
3684<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
3685</ul>
3686<p>Decodes <code>buf</code> to a string according to the specified character encoding in
3687<code>encoding</code>. <code>start</code> and <code>end</code> may be passed to decode only a subset of <code>buf</code>.</p>
3688<p>If <code>encoding</code> is <code>'utf8'</code> and a byte sequence in the input is not valid UTF-8,
3689then each invalid byte is replaced with the replacement character <code>U+FFFD</code>.</p>
3690<p>The maximum length of a string instance (in UTF-16 code units) is available
3691as <a href="#bufferconstantsmax_string_length"><code>buffer.constants.MAX_STRING_LENGTH</code></a>.</p>
3692
3693<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3694
3695<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">26</span>);
3696
3697<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &#x3C; <span class="hljs-number">26</span>; i++) {
3698  <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
3699  buf1[i] = i + <span class="hljs-number">97</span>;
3700}
3701
3702<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>));
3703<span class="hljs-comment">// Prints: abcdefghijklmnopqrstuvwxyz</span>
3704<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>, <span class="hljs-number">0</span>, <span class="hljs-number">5</span>));
3705<span class="hljs-comment">// Prints: abcde</span>
3706
3707<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'tést'</span>);
3708
3709<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
3710<span class="hljs-comment">// Prints: 74c3a97374</span>
3711<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>, <span class="hljs-number">0</span>, <span class="hljs-number">3</span>));
3712<span class="hljs-comment">// Prints: té</span>
3713<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-literal">undefined</span>, <span class="hljs-number">0</span>, <span class="hljs-number">3</span>));
3714<span class="hljs-comment">// Prints: té</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3715
3716<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">26</span>);
3717
3718<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &#x3C; <span class="hljs-number">26</span>; i++) {
3719  <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
3720  buf1[i] = i + <span class="hljs-number">97</span>;
3721}
3722
3723<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>));
3724<span class="hljs-comment">// Prints: abcdefghijklmnopqrstuvwxyz</span>
3725<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>, <span class="hljs-number">0</span>, <span class="hljs-number">5</span>));
3726<span class="hljs-comment">// Prints: abcde</span>
3727
3728<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'tést'</span>);
3729
3730<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'hex'</span>));
3731<span class="hljs-comment">// Prints: 74c3a97374</span>
3732<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'utf8'</span>, <span class="hljs-number">0</span>, <span class="hljs-number">3</span>));
3733<span class="hljs-comment">// Prints: té</span>
3734<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>(<span class="hljs-literal">undefined</span>, <span class="hljs-number">0</span>, <span class="hljs-number">3</span>));
3735<span class="hljs-comment">// Prints: té</span></code><button class="copy-button">copy</button></pre>
3736<h4><code>buf.values()</code><span><a class="mark" href="#bufvalues" id="bufvalues">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_values"></a></h4>
3737<div class="api_metadata">
3738<span>Added in: v1.1.0</span>
3739</div>
3740<ul>
3741<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol" class="type">&#x3C;Iterator></a></li>
3742</ul>
3743<p>Creates and returns an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols">iterator</a> for <code>buf</code> values (bytes). This function is
3744called automatically when a <code>Buffer</code> is used in a <code>for..of</code> statement.</p>
3745
3746<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3747
3748<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>);
3749
3750<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> value <span class="hljs-keyword">of</span> buf.<span class="hljs-title function_">values</span>()) {
3751  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value);
3752}
3753<span class="hljs-comment">// Prints:</span>
3754<span class="hljs-comment">//   98</span>
3755<span class="hljs-comment">//   117</span>
3756<span class="hljs-comment">//   102</span>
3757<span class="hljs-comment">//   102</span>
3758<span class="hljs-comment">//   101</span>
3759<span class="hljs-comment">//   114</span>
3760
3761<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> value <span class="hljs-keyword">of</span> buf) {
3762  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value);
3763}
3764<span class="hljs-comment">// Prints:</span>
3765<span class="hljs-comment">//   98</span>
3766<span class="hljs-comment">//   117</span>
3767<span class="hljs-comment">//   102</span>
3768<span class="hljs-comment">//   102</span>
3769<span class="hljs-comment">//   101</span>
3770<span class="hljs-comment">//   114</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3771
3772<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'buffer'</span>);
3773
3774<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> value <span class="hljs-keyword">of</span> buf.<span class="hljs-title function_">values</span>()) {
3775  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value);
3776}
3777<span class="hljs-comment">// Prints:</span>
3778<span class="hljs-comment">//   98</span>
3779<span class="hljs-comment">//   117</span>
3780<span class="hljs-comment">//   102</span>
3781<span class="hljs-comment">//   102</span>
3782<span class="hljs-comment">//   101</span>
3783<span class="hljs-comment">//   114</span>
3784
3785<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> value <span class="hljs-keyword">of</span> buf) {
3786  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value);
3787}
3788<span class="hljs-comment">// Prints:</span>
3789<span class="hljs-comment">//   98</span>
3790<span class="hljs-comment">//   117</span>
3791<span class="hljs-comment">//   102</span>
3792<span class="hljs-comment">//   102</span>
3793<span class="hljs-comment">//   101</span>
3794<span class="hljs-comment">//   114</span></code><button class="copy-button">copy</button></pre>
3795<h4><code>buf.write(string[, offset[, length]][, encoding])</code><span><a class="mark" href="#bufwritestring-offset-length-encoding" id="bufwritestring-offset-length-encoding">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_write_string_offset_length_encoding"></a></h4>
3796<div class="api_metadata">
3797<span>Added in: v0.1.90</span>
3798</div>
3799<ul>
3800<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> String to write to <code>buf</code>.</li>
3801<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write <code>string</code>.
3802<strong>Default:</strong> <code>0</code>.</li>
3803<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Maximum number of bytes to write (written bytes will not
3804exceed <code>buf.length - offset</code>). <strong>Default:</strong> <code>buf.length - offset</code>.</li>
3805<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The character encoding of <code>string</code>. <strong>Default:</strong> <code>'utf8'</code>.</li>
3806<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes written.</li>
3807</ul>
3808<p>Writes <code>string</code> to <code>buf</code> at <code>offset</code> according to the character encoding in
3809<code>encoding</code>. The <code>length</code> parameter is the number of bytes to write. If <code>buf</code> did
3810not contain enough space to fit the entire string, only part of <code>string</code> will be
3811written. However, partially encoded characters will not be written.</p>
3812
3813<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3814
3815<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">256</span>);
3816
3817<span class="hljs-keyword">const</span> len = buf.<span class="hljs-title function_">write</span>(<span class="hljs-string">'\u00bd + \u00bc = \u00be'</span>, <span class="hljs-number">0</span>);
3818
3819<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${len}</span> bytes: <span class="hljs-subst">${buf.toString(<span class="hljs-string">'utf8'</span>, <span class="hljs-number">0</span>, len)}</span>`</span>);
3820<span class="hljs-comment">// Prints: 12 bytes: ½ + ¼ = ¾</span>
3821
3822<span class="hljs-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">10</span>);
3823
3824<span class="hljs-keyword">const</span> length = buffer.<span class="hljs-title function_">write</span>(<span class="hljs-string">'abcd'</span>, <span class="hljs-number">8</span>);
3825
3826<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${length}</span> bytes: <span class="hljs-subst">${buffer.toString(<span class="hljs-string">'utf8'</span>, <span class="hljs-number">8</span>, <span class="hljs-number">10</span>)}</span>`</span>);
3827<span class="hljs-comment">// Prints: 2 bytes : ab</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3828
3829<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">256</span>);
3830
3831<span class="hljs-keyword">const</span> len = buf.<span class="hljs-title function_">write</span>(<span class="hljs-string">'\u00bd + \u00bc = \u00be'</span>, <span class="hljs-number">0</span>);
3832
3833<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${len}</span> bytes: <span class="hljs-subst">${buf.toString(<span class="hljs-string">'utf8'</span>, <span class="hljs-number">0</span>, len)}</span>`</span>);
3834<span class="hljs-comment">// Prints: 12 bytes: ½ + ¼ = ¾</span>
3835
3836<span class="hljs-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">alloc</span>(<span class="hljs-number">10</span>);
3837
3838<span class="hljs-keyword">const</span> length = buffer.<span class="hljs-title function_">write</span>(<span class="hljs-string">'abcd'</span>, <span class="hljs-number">8</span>);
3839
3840<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`<span class="hljs-subst">${length}</span> bytes: <span class="hljs-subst">${buffer.toString(<span class="hljs-string">'utf8'</span>, <span class="hljs-number">8</span>, <span class="hljs-number">10</span>)}</span>`</span>);
3841<span class="hljs-comment">// Prints: 2 bytes : ab</span></code><button class="copy-button">copy</button></pre>
3842<h4><code>buf.writeBigInt64BE(value[, offset])</code><span><a class="mark" href="#bufwritebigint64bevalue-offset" id="bufwritebigint64bevalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writebigint64be_value_offset"></a></h4>
3843<div class="api_metadata">
3844<span>Added in: v12.0.0, v10.20.0</span>
3845</div>
3846<ul>
3847<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a> Number to be written to <code>buf</code>.</li>
3848<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write. Must
3849satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
3850<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <code>offset</code> plus the number of bytes written.</li>
3851</ul>
3852<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian.</p>
3853<p><code>value</code> is interpreted and written as a two's complement signed integer.</p>
3854
3855<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3856
3857<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>);
3858
3859buf.<span class="hljs-title function_">writeBigInt64BE</span>(<span class="hljs-number">0x0102030405060708n</span>, <span class="hljs-number">0</span>);
3860
3861<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
3862<span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3863
3864<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>);
3865
3866buf.<span class="hljs-title function_">writeBigInt64BE</span>(<span class="hljs-number">0x0102030405060708n</span>, <span class="hljs-number">0</span>);
3867
3868<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
3869<span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span></code><button class="copy-button">copy</button></pre>
3870<h4><code>buf.writeBigInt64LE(value[, offset])</code><span><a class="mark" href="#bufwritebigint64levalue-offset" id="bufwritebigint64levalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writebigint64le_value_offset"></a></h4>
3871<div class="api_metadata">
3872<span>Added in: v12.0.0, v10.20.0</span>
3873</div>
3874<ul>
3875<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a> Number to be written to <code>buf</code>.</li>
3876<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write. Must
3877satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
3878<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <code>offset</code> plus the number of bytes written.</li>
3879</ul>
3880<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian.</p>
3881<p><code>value</code> is interpreted and written as a two's complement signed integer.</p>
3882
3883<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3884
3885<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>);
3886
3887buf.<span class="hljs-title function_">writeBigInt64LE</span>(<span class="hljs-number">0x0102030405060708n</span>, <span class="hljs-number">0</span>);
3888
3889<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
3890<span class="hljs-comment">// Prints: &#x3C;Buffer 08 07 06 05 04 03 02 01></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3891
3892<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>);
3893
3894buf.<span class="hljs-title function_">writeBigInt64LE</span>(<span class="hljs-number">0x0102030405060708n</span>, <span class="hljs-number">0</span>);
3895
3896<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
3897<span class="hljs-comment">// Prints: &#x3C;Buffer 08 07 06 05 04 03 02 01></span></code><button class="copy-button">copy</button></pre>
3898<h4><code>buf.writeBigUInt64BE(value[, offset])</code><span><a class="mark" href="#bufwritebiguint64bevalue-offset" id="bufwritebiguint64bevalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writebiguint64be_value_offset"></a></h4>
3899<div class="api_metadata">
3900<details class="changelog"><summary>History</summary>
3901<table>
3902<tbody><tr><th>Version</th><th>Changes</th></tr>
3903<tr><td>v14.10.0, v12.19.0</td>
3904<td><p>This function is also available as <code>buf.writeBigUint64BE()</code>.</p></td></tr>
3905<tr><td>v12.0.0, v10.20.0</td>
3906<td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr>
3907</tbody></table>
3908</details>
3909</div>
3910<ul>
3911<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a> Number to be written to <code>buf</code>.</li>
3912<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write. Must
3913satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
3914<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <code>offset</code> plus the number of bytes written.</li>
3915</ul>
3916<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian.</p>
3917<p>This function is also available under the <code>writeBigUint64BE</code> alias.</p>
3918
3919<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3920
3921<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>);
3922
3923buf.<span class="hljs-title function_">writeBigUInt64BE</span>(<span class="hljs-number">0xdecafafecacefaden</span>, <span class="hljs-number">0</span>);
3924
3925<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
3926<span class="hljs-comment">// Prints: &#x3C;Buffer de ca fa fe ca ce fa de></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3927
3928<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>);
3929
3930buf.<span class="hljs-title function_">writeBigUInt64BE</span>(<span class="hljs-number">0xdecafafecacefaden</span>, <span class="hljs-number">0</span>);
3931
3932<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
3933<span class="hljs-comment">// Prints: &#x3C;Buffer de ca fa fe ca ce fa de></span></code><button class="copy-button">copy</button></pre>
3934<h4><code>buf.writeBigUInt64LE(value[, offset])</code><span><a class="mark" href="#bufwritebiguint64levalue-offset" id="bufwritebiguint64levalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writebiguint64le_value_offset"></a></h4>
3935<div class="api_metadata">
3936<details class="changelog"><summary>History</summary>
3937<table>
3938<tbody><tr><th>Version</th><th>Changes</th></tr>
3939<tr><td>v14.10.0, v12.19.0</td>
3940<td><p>This function is also available as <code>buf.writeBigUint64LE()</code>.</p></td></tr>
3941<tr><td>v12.0.0, v10.20.0</td>
3942<td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr>
3943</tbody></table>
3944</details>
3945</div>
3946<ul>
3947<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a> Number to be written to <code>buf</code>.</li>
3948<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write. Must
3949satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
3950<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <code>offset</code> plus the number of bytes written.</li>
3951</ul>
3952<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian</p>
3953
3954<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3955
3956<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>);
3957
3958buf.<span class="hljs-title function_">writeBigUInt64LE</span>(<span class="hljs-number">0xdecafafecacefaden</span>, <span class="hljs-number">0</span>);
3959
3960<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
3961<span class="hljs-comment">// Prints: &#x3C;Buffer de fa ce ca fe fa ca de></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
3962
3963<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>);
3964
3965buf.<span class="hljs-title function_">writeBigUInt64LE</span>(<span class="hljs-number">0xdecafafecacefaden</span>, <span class="hljs-number">0</span>);
3966
3967<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
3968<span class="hljs-comment">// Prints: &#x3C;Buffer de fa ce ca fe fa ca de></span></code><button class="copy-button">copy</button></pre>
3969<p>This function is also available under the <code>writeBigUint64LE</code> alias.</p>
3970<h4><code>buf.writeDoubleBE(value[, offset])</code><span><a class="mark" href="#bufwritedoublebevalue-offset" id="bufwritedoublebevalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writedoublebe_value_offset"></a></h4>
3971<div class="api_metadata">
3972<details class="changelog"><summary>History</summary>
3973<table>
3974<tbody><tr><th>Version</th><th>Changes</th></tr>
3975<tr><td>v10.0.0</td>
3976<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
3977<tr><td>v0.11.15</td>
3978<td><p><span>Added in: v0.11.15</span></p></td></tr>
3979</tbody></table>
3980</details>
3981</div>
3982<ul>
3983<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Number to be written to <code>buf</code>.</li>
3984<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write. Must
3985satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
3986<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <code>offset</code> plus the number of bytes written.</li>
3987</ul>
3988<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code>
3989must be a JavaScript number. Behavior is undefined when <code>value</code> is anything
3990other than a JavaScript number.</p>
3991
3992<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
3993
3994<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>);
3995
3996buf.<span class="hljs-title function_">writeDoubleBE</span>(<span class="hljs-number">123.456</span>, <span class="hljs-number">0</span>);
3997
3998<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
3999<span class="hljs-comment">// Prints: &#x3C;Buffer 40 5e dd 2f 1a 9f be 77></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
4000
4001<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>);
4002
4003buf.<span class="hljs-title function_">writeDoubleBE</span>(<span class="hljs-number">123.456</span>, <span class="hljs-number">0</span>);
4004
4005<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4006<span class="hljs-comment">// Prints: &#x3C;Buffer 40 5e dd 2f 1a 9f be 77></span></code><button class="copy-button">copy</button></pre>
4007<h4><code>buf.writeDoubleLE(value[, offset])</code><span><a class="mark" href="#bufwritedoublelevalue-offset" id="bufwritedoublelevalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writedoublele_value_offset"></a></h4>
4008<div class="api_metadata">
4009<details class="changelog"><summary>History</summary>
4010<table>
4011<tbody><tr><th>Version</th><th>Changes</th></tr>
4012<tr><td>v10.0.0</td>
4013<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
4014<tr><td>v0.11.15</td>
4015<td><p><span>Added in: v0.11.15</span></p></td></tr>
4016</tbody></table>
4017</details>
4018</div>
4019<ul>
4020<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Number to be written to <code>buf</code>.</li>
4021<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write. Must
4022satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
4023<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <code>offset</code> plus the number of bytes written.</li>
4024</ul>
4025<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code>
4026must be a JavaScript number. Behavior is undefined when <code>value</code> is anything
4027other than a JavaScript number.</p>
4028
4029<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
4030
4031<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>);
4032
4033buf.<span class="hljs-title function_">writeDoubleLE</span>(<span class="hljs-number">123.456</span>, <span class="hljs-number">0</span>);
4034
4035<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4036<span class="hljs-comment">// Prints: &#x3C;Buffer 77 be 9f 1a 2f dd 5e 40></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
4037
4038<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">8</span>);
4039
4040buf.<span class="hljs-title function_">writeDoubleLE</span>(<span class="hljs-number">123.456</span>, <span class="hljs-number">0</span>);
4041
4042<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4043<span class="hljs-comment">// Prints: &#x3C;Buffer 77 be 9f 1a 2f dd 5e 40></span></code><button class="copy-button">copy</button></pre>
4044<h4><code>buf.writeFloatBE(value[, offset])</code><span><a class="mark" href="#bufwritefloatbevalue-offset" id="bufwritefloatbevalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writefloatbe_value_offset"></a></h4>
4045<div class="api_metadata">
4046<details class="changelog"><summary>History</summary>
4047<table>
4048<tbody><tr><th>Version</th><th>Changes</th></tr>
4049<tr><td>v10.0.0</td>
4050<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
4051<tr><td>v0.11.15</td>
4052<td><p><span>Added in: v0.11.15</span></p></td></tr>
4053</tbody></table>
4054</details>
4055</div>
4056<ul>
4057<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Number to be written to <code>buf</code>.</li>
4058<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write. Must
4059satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
4060<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <code>offset</code> plus the number of bytes written.</li>
4061</ul>
4062<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. Behavior is
4063undefined when <code>value</code> is anything other than a JavaScript number.</p>
4064
4065<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
4066
4067<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>);
4068
4069buf.<span class="hljs-title function_">writeFloatBE</span>(<span class="hljs-number">0xcafebabe</span>, <span class="hljs-number">0</span>);
4070
4071<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4072<span class="hljs-comment">// Prints: &#x3C;Buffer 4f 4a fe bb></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
4073
4074<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>);
4075
4076buf.<span class="hljs-title function_">writeFloatBE</span>(<span class="hljs-number">0xcafebabe</span>, <span class="hljs-number">0</span>);
4077
4078<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4079<span class="hljs-comment">// Prints: &#x3C;Buffer 4f 4a fe bb></span></code><button class="copy-button">copy</button></pre>
4080<h4><code>buf.writeFloatLE(value[, offset])</code><span><a class="mark" href="#bufwritefloatlevalue-offset" id="bufwritefloatlevalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writefloatle_value_offset"></a></h4>
4081<div class="api_metadata">
4082<details class="changelog"><summary>History</summary>
4083<table>
4084<tbody><tr><th>Version</th><th>Changes</th></tr>
4085<tr><td>v10.0.0</td>
4086<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
4087<tr><td>v0.11.15</td>
4088<td><p><span>Added in: v0.11.15</span></p></td></tr>
4089</tbody></table>
4090</details>
4091</div>
4092<ul>
4093<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Number to be written to <code>buf</code>.</li>
4094<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write. Must
4095satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
4096<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <code>offset</code> plus the number of bytes written.</li>
4097</ul>
4098<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. Behavior is
4099undefined when <code>value</code> is anything other than a JavaScript number.</p>
4100
4101<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
4102
4103<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>);
4104
4105buf.<span class="hljs-title function_">writeFloatLE</span>(<span class="hljs-number">0xcafebabe</span>, <span class="hljs-number">0</span>);
4106
4107<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4108<span class="hljs-comment">// Prints: &#x3C;Buffer bb fe 4a 4f></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
4109
4110<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>);
4111
4112buf.<span class="hljs-title function_">writeFloatLE</span>(<span class="hljs-number">0xcafebabe</span>, <span class="hljs-number">0</span>);
4113
4114<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4115<span class="hljs-comment">// Prints: &#x3C;Buffer bb fe 4a 4f></span></code><button class="copy-button">copy</button></pre>
4116<h4><code>buf.writeInt8(value[, offset])</code><span><a class="mark" href="#bufwriteint8value-offset" id="bufwriteint8value-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeint8_value_offset"></a></h4>
4117<div class="api_metadata">
4118<details class="changelog"><summary>History</summary>
4119<table>
4120<tbody><tr><th>Version</th><th>Changes</th></tr>
4121<tr><td>v10.0.0</td>
4122<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
4123<tr><td>v0.5.0</td>
4124<td><p><span>Added in: v0.5.0</span></p></td></tr>
4125</tbody></table>
4126</details>
4127</div>
4128<ul>
4129<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number to be written to <code>buf</code>.</li>
4130<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write. Must
4131satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li>
4132<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <code>offset</code> plus the number of bytes written.</li>
4133</ul>
4134<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code>. <code>value</code> must be a valid
4135signed 8-bit integer. Behavior is undefined when <code>value</code> is anything other than
4136a signed 8-bit integer.</p>
4137<p><code>value</code> is interpreted and written as a two's complement signed integer.</p>
4138
4139<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
4140
4141<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">2</span>);
4142
4143buf.<span class="hljs-title function_">writeInt8</span>(<span class="hljs-number">2</span>, <span class="hljs-number">0</span>);
4144buf.<span class="hljs-title function_">writeInt8</span>(-<span class="hljs-number">2</span>, <span class="hljs-number">1</span>);
4145
4146<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4147<span class="hljs-comment">// Prints: &#x3C;Buffer 02 fe></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
4148
4149<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">2</span>);
4150
4151buf.<span class="hljs-title function_">writeInt8</span>(<span class="hljs-number">2</span>, <span class="hljs-number">0</span>);
4152buf.<span class="hljs-title function_">writeInt8</span>(-<span class="hljs-number">2</span>, <span class="hljs-number">1</span>);
4153
4154<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4155<span class="hljs-comment">// Prints: &#x3C;Buffer 02 fe></span></code><button class="copy-button">copy</button></pre>
4156<h4><code>buf.writeInt16BE(value[, offset])</code><span><a class="mark" href="#bufwriteint16bevalue-offset" id="bufwriteint16bevalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeint16be_value_offset"></a></h4>
4157<div class="api_metadata">
4158<details class="changelog"><summary>History</summary>
4159<table>
4160<tbody><tr><th>Version</th><th>Changes</th></tr>
4161<tr><td>v10.0.0</td>
4162<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
4163<tr><td>v0.5.5</td>
4164<td><p><span>Added in: v0.5.5</span></p></td></tr>
4165</tbody></table>
4166</details>
4167</div>
4168<ul>
4169<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number to be written to <code>buf</code>.</li>
4170<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write. Must
4171satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li>
4172<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <code>offset</code> plus the number of bytes written.</li>
4173</ul>
4174<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian.  The <code>value</code>
4175must be a valid signed 16-bit integer. Behavior is undefined when <code>value</code> is
4176anything other than a signed 16-bit integer.</p>
4177<p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p>
4178
4179<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
4180
4181<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">2</span>);
4182
4183buf.<span class="hljs-title function_">writeInt16BE</span>(<span class="hljs-number">0x0102</span>, <span class="hljs-number">0</span>);
4184
4185<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4186<span class="hljs-comment">// Prints: &#x3C;Buffer 01 02></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
4187
4188<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">2</span>);
4189
4190buf.<span class="hljs-title function_">writeInt16BE</span>(<span class="hljs-number">0x0102</span>, <span class="hljs-number">0</span>);
4191
4192<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4193<span class="hljs-comment">// Prints: &#x3C;Buffer 01 02></span></code><button class="copy-button">copy</button></pre>
4194<h4><code>buf.writeInt16LE(value[, offset])</code><span><a class="mark" href="#bufwriteint16levalue-offset" id="bufwriteint16levalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeint16le_value_offset"></a></h4>
4195<div class="api_metadata">
4196<details class="changelog"><summary>History</summary>
4197<table>
4198<tbody><tr><th>Version</th><th>Changes</th></tr>
4199<tr><td>v10.0.0</td>
4200<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
4201<tr><td>v0.5.5</td>
4202<td><p><span>Added in: v0.5.5</span></p></td></tr>
4203</tbody></table>
4204</details>
4205</div>
4206<ul>
4207<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number to be written to <code>buf</code>.</li>
4208<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write. Must
4209satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li>
4210<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <code>offset</code> plus the number of bytes written.</li>
4211</ul>
4212<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian.  The <code>value</code>
4213must be a valid signed 16-bit integer. Behavior is undefined when <code>value</code> is
4214anything other than a signed 16-bit integer.</p>
4215<p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p>
4216
4217<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
4218
4219<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">2</span>);
4220
4221buf.<span class="hljs-title function_">writeInt16LE</span>(<span class="hljs-number">0x0304</span>, <span class="hljs-number">0</span>);
4222
4223<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4224<span class="hljs-comment">// Prints: &#x3C;Buffer 04 03></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
4225
4226<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">2</span>);
4227
4228buf.<span class="hljs-title function_">writeInt16LE</span>(<span class="hljs-number">0x0304</span>, <span class="hljs-number">0</span>);
4229
4230<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4231<span class="hljs-comment">// Prints: &#x3C;Buffer 04 03></span></code><button class="copy-button">copy</button></pre>
4232<h4><code>buf.writeInt32BE(value[, offset])</code><span><a class="mark" href="#bufwriteint32bevalue-offset" id="bufwriteint32bevalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeint32be_value_offset"></a></h4>
4233<div class="api_metadata">
4234<details class="changelog"><summary>History</summary>
4235<table>
4236<tbody><tr><th>Version</th><th>Changes</th></tr>
4237<tr><td>v10.0.0</td>
4238<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
4239<tr><td>v0.5.5</td>
4240<td><p><span>Added in: v0.5.5</span></p></td></tr>
4241</tbody></table>
4242</details>
4243</div>
4244<ul>
4245<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number to be written to <code>buf</code>.</li>
4246<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write. Must
4247satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
4248<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <code>offset</code> plus the number of bytes written.</li>
4249</ul>
4250<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code>
4251must be a valid signed 32-bit integer. Behavior is undefined when <code>value</code> is
4252anything other than a signed 32-bit integer.</p>
4253<p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p>
4254
4255<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
4256
4257<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>);
4258
4259buf.<span class="hljs-title function_">writeInt32BE</span>(<span class="hljs-number">0x01020304</span>, <span class="hljs-number">0</span>);
4260
4261<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4262<span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
4263
4264<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>);
4265
4266buf.<span class="hljs-title function_">writeInt32BE</span>(<span class="hljs-number">0x01020304</span>, <span class="hljs-number">0</span>);
4267
4268<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4269<span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04></span></code><button class="copy-button">copy</button></pre>
4270<h4><code>buf.writeInt32LE(value[, offset])</code><span><a class="mark" href="#bufwriteint32levalue-offset" id="bufwriteint32levalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeint32le_value_offset"></a></h4>
4271<div class="api_metadata">
4272<details class="changelog"><summary>History</summary>
4273<table>
4274<tbody><tr><th>Version</th><th>Changes</th></tr>
4275<tr><td>v10.0.0</td>
4276<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
4277<tr><td>v0.5.5</td>
4278<td><p><span>Added in: v0.5.5</span></p></td></tr>
4279</tbody></table>
4280</details>
4281</div>
4282<ul>
4283<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number to be written to <code>buf</code>.</li>
4284<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write. Must
4285satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
4286<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <code>offset</code> plus the number of bytes written.</li>
4287</ul>
4288<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code>
4289must be a valid signed 32-bit integer. Behavior is undefined when <code>value</code> is
4290anything other than a signed 32-bit integer.</p>
4291<p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p>
4292
4293<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
4294
4295<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>);
4296
4297buf.<span class="hljs-title function_">writeInt32LE</span>(<span class="hljs-number">0x05060708</span>, <span class="hljs-number">0</span>);
4298
4299<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4300<span class="hljs-comment">// Prints: &#x3C;Buffer 08 07 06 05></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
4301
4302<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>);
4303
4304buf.<span class="hljs-title function_">writeInt32LE</span>(<span class="hljs-number">0x05060708</span>, <span class="hljs-number">0</span>);
4305
4306<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4307<span class="hljs-comment">// Prints: &#x3C;Buffer 08 07 06 05></span></code><button class="copy-button">copy</button></pre>
4308<h4><code>buf.writeIntBE(value, offset, byteLength)</code><span><a class="mark" href="#bufwriteintbevalue-offset-bytelength" id="bufwriteintbevalue-offset-bytelength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeintbe_value_offset_bytelength"></a></h4>
4309<div class="api_metadata">
4310<details class="changelog"><summary>History</summary>
4311<table>
4312<tbody><tr><th>Version</th><th>Changes</th></tr>
4313<tr><td>v10.0.0</td>
4314<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset and <code>byteLength</code> to <code>uint32</code> anymore.</p></td></tr>
4315<tr><td>v0.11.15</td>
4316<td><p><span>Added in: v0.11.15</span></p></td></tr>
4317</tbody></table>
4318</details>
4319</div>
4320<ul>
4321<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number to be written to <code>buf</code>.</li>
4322<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write. Must
4323satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
4324<li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to write. Must satisfy
4325<code>0 &#x3C; byteLength &#x3C;= 6</code>.</li>
4326<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <code>offset</code> plus the number of bytes written.</li>
4327</ul>
4328<p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code>
4329as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined when
4330<code>value</code> is anything other than a signed integer.</p>
4331
4332<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
4333
4334<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">6</span>);
4335
4336buf.<span class="hljs-title function_">writeIntBE</span>(<span class="hljs-number">0x1234567890ab</span>, <span class="hljs-number">0</span>, <span class="hljs-number">6</span>);
4337
4338<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4339<span class="hljs-comment">// Prints: &#x3C;Buffer 12 34 56 78 90 ab></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
4340
4341<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">6</span>);
4342
4343buf.<span class="hljs-title function_">writeIntBE</span>(<span class="hljs-number">0x1234567890ab</span>, <span class="hljs-number">0</span>, <span class="hljs-number">6</span>);
4344
4345<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4346<span class="hljs-comment">// Prints: &#x3C;Buffer 12 34 56 78 90 ab></span></code><button class="copy-button">copy</button></pre>
4347<h4><code>buf.writeIntLE(value, offset, byteLength)</code><span><a class="mark" href="#bufwriteintlevalue-offset-bytelength" id="bufwriteintlevalue-offset-bytelength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeintle_value_offset_bytelength"></a></h4>
4348<div class="api_metadata">
4349<details class="changelog"><summary>History</summary>
4350<table>
4351<tbody><tr><th>Version</th><th>Changes</th></tr>
4352<tr><td>v10.0.0</td>
4353<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset and <code>byteLength</code> to <code>uint32</code> anymore.</p></td></tr>
4354<tr><td>v0.11.15</td>
4355<td><p><span>Added in: v0.11.15</span></p></td></tr>
4356</tbody></table>
4357</details>
4358</div>
4359<ul>
4360<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number to be written to <code>buf</code>.</li>
4361<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write. Must
4362satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
4363<li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to write. Must satisfy
4364<code>0 &#x3C; byteLength &#x3C;= 6</code>.</li>
4365<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <code>offset</code> plus the number of bytes written.</li>
4366</ul>
4367<p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code>
4368as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined
4369when <code>value</code> is anything other than a signed integer.</p>
4370
4371<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
4372
4373<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">6</span>);
4374
4375buf.<span class="hljs-title function_">writeIntLE</span>(<span class="hljs-number">0x1234567890ab</span>, <span class="hljs-number">0</span>, <span class="hljs-number">6</span>);
4376
4377<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4378<span class="hljs-comment">// Prints: &#x3C;Buffer ab 90 78 56 34 12></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
4379
4380<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">6</span>);
4381
4382buf.<span class="hljs-title function_">writeIntLE</span>(<span class="hljs-number">0x1234567890ab</span>, <span class="hljs-number">0</span>, <span class="hljs-number">6</span>);
4383
4384<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4385<span class="hljs-comment">// Prints: &#x3C;Buffer ab 90 78 56 34 12></span></code><button class="copy-button">copy</button></pre>
4386<h4><code>buf.writeUInt8(value[, offset])</code><span><a class="mark" href="#bufwriteuint8value-offset" id="bufwriteuint8value-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeuint8_value_offset"></a></h4>
4387<div class="api_metadata">
4388<details class="changelog"><summary>History</summary>
4389<table>
4390<tbody><tr><th>Version</th><th>Changes</th></tr>
4391<tr><td>v14.9.0, v12.19.0</td>
4392<td><p>This function is also available as <code>buf.writeUint8()</code>.</p></td></tr>
4393<tr><td>v10.0.0</td>
4394<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
4395<tr><td>v0.5.0</td>
4396<td><p><span>Added in: v0.5.0</span></p></td></tr>
4397</tbody></table>
4398</details>
4399</div>
4400<ul>
4401<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number to be written to <code>buf</code>.</li>
4402<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write. Must
4403satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li>
4404<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <code>offset</code> plus the number of bytes written.</li>
4405</ul>
4406<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code>. <code>value</code> must be a
4407valid unsigned 8-bit integer. Behavior is undefined when <code>value</code> is anything
4408other than an unsigned 8-bit integer.</p>
4409<p>This function is also available under the <code>writeUint8</code> alias.</p>
4410
4411<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
4412
4413<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>);
4414
4415buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x3</span>, <span class="hljs-number">0</span>);
4416buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x4</span>, <span class="hljs-number">1</span>);
4417buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x23</span>, <span class="hljs-number">2</span>);
4418buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x42</span>, <span class="hljs-number">3</span>);
4419
4420<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4421<span class="hljs-comment">// Prints: &#x3C;Buffer 03 04 23 42></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
4422
4423<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>);
4424
4425buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x3</span>, <span class="hljs-number">0</span>);
4426buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x4</span>, <span class="hljs-number">1</span>);
4427buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x23</span>, <span class="hljs-number">2</span>);
4428buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x42</span>, <span class="hljs-number">3</span>);
4429
4430<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4431<span class="hljs-comment">// Prints: &#x3C;Buffer 03 04 23 42></span></code><button class="copy-button">copy</button></pre>
4432<h4><code>buf.writeUInt16BE(value[, offset])</code><span><a class="mark" href="#bufwriteuint16bevalue-offset" id="bufwriteuint16bevalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeuint16be_value_offset"></a></h4>
4433<div class="api_metadata">
4434<details class="changelog"><summary>History</summary>
4435<table>
4436<tbody><tr><th>Version</th><th>Changes</th></tr>
4437<tr><td>v14.9.0, v12.19.0</td>
4438<td><p>This function is also available as <code>buf.writeUint16BE()</code>.</p></td></tr>
4439<tr><td>v10.0.0</td>
4440<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
4441<tr><td>v0.5.5</td>
4442<td><p><span>Added in: v0.5.5</span></p></td></tr>
4443</tbody></table>
4444</details>
4445</div>
4446<ul>
4447<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number to be written to <code>buf</code>.</li>
4448<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write. Must
4449satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li>
4450<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <code>offset</code> plus the number of bytes written.</li>
4451</ul>
4452<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code>
4453must be a valid unsigned 16-bit integer. Behavior is undefined when <code>value</code>
4454is anything other than an unsigned 16-bit integer.</p>
4455<p>This function is also available under the <code>writeUint16BE</code> alias.</p>
4456
4457<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
4458
4459<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>);
4460
4461buf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xdead</span>, <span class="hljs-number">0</span>);
4462buf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xbeef</span>, <span class="hljs-number">2</span>);
4463
4464<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4465<span class="hljs-comment">// Prints: &#x3C;Buffer de ad be ef></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
4466
4467<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>);
4468
4469buf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xdead</span>, <span class="hljs-number">0</span>);
4470buf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xbeef</span>, <span class="hljs-number">2</span>);
4471
4472<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4473<span class="hljs-comment">// Prints: &#x3C;Buffer de ad be ef></span></code><button class="copy-button">copy</button></pre>
4474<h4><code>buf.writeUInt16LE(value[, offset])</code><span><a class="mark" href="#bufwriteuint16levalue-offset" id="bufwriteuint16levalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeuint16le_value_offset"></a></h4>
4475<div class="api_metadata">
4476<details class="changelog"><summary>History</summary>
4477<table>
4478<tbody><tr><th>Version</th><th>Changes</th></tr>
4479<tr><td>v14.9.0, v12.19.0</td>
4480<td><p>This function is also available as <code>buf.writeUint16LE()</code>.</p></td></tr>
4481<tr><td>v10.0.0</td>
4482<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
4483<tr><td>v0.5.5</td>
4484<td><p><span>Added in: v0.5.5</span></p></td></tr>
4485</tbody></table>
4486</details>
4487</div>
4488<ul>
4489<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number to be written to <code>buf</code>.</li>
4490<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write. Must
4491satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li>
4492<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <code>offset</code> plus the number of bytes written.</li>
4493</ul>
4494<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code>
4495must be a valid unsigned 16-bit integer. Behavior is undefined when <code>value</code> is
4496anything other than an unsigned 16-bit integer.</p>
4497<p>This function is also available under the <code>writeUint16LE</code> alias.</p>
4498
4499<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
4500
4501<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>);
4502
4503buf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xdead</span>, <span class="hljs-number">0</span>);
4504buf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xbeef</span>, <span class="hljs-number">2</span>);
4505
4506<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4507<span class="hljs-comment">// Prints: &#x3C;Buffer ad de ef be></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
4508
4509<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>);
4510
4511buf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xdead</span>, <span class="hljs-number">0</span>);
4512buf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xbeef</span>, <span class="hljs-number">2</span>);
4513
4514<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4515<span class="hljs-comment">// Prints: &#x3C;Buffer ad de ef be></span></code><button class="copy-button">copy</button></pre>
4516<h4><code>buf.writeUInt32BE(value[, offset])</code><span><a class="mark" href="#bufwriteuint32bevalue-offset" id="bufwriteuint32bevalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeuint32be_value_offset"></a></h4>
4517<div class="api_metadata">
4518<details class="changelog"><summary>History</summary>
4519<table>
4520<tbody><tr><th>Version</th><th>Changes</th></tr>
4521<tr><td>v14.9.0, v12.19.0</td>
4522<td><p>This function is also available as <code>buf.writeUint32BE()</code>.</p></td></tr>
4523<tr><td>v10.0.0</td>
4524<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
4525<tr><td>v0.5.5</td>
4526<td><p><span>Added in: v0.5.5</span></p></td></tr>
4527</tbody></table>
4528</details>
4529</div>
4530<ul>
4531<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number to be written to <code>buf</code>.</li>
4532<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write. Must
4533satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
4534<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <code>offset</code> plus the number of bytes written.</li>
4535</ul>
4536<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code>
4537must be a valid unsigned 32-bit integer. Behavior is undefined when <code>value</code>
4538is anything other than an unsigned 32-bit integer.</p>
4539<p>This function is also available under the <code>writeUint32BE</code> alias.</p>
4540
4541<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
4542
4543<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>);
4544
4545buf.<span class="hljs-title function_">writeUInt32BE</span>(<span class="hljs-number">0xfeedface</span>, <span class="hljs-number">0</span>);
4546
4547<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4548<span class="hljs-comment">// Prints: &#x3C;Buffer fe ed fa ce></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
4549
4550<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>);
4551
4552buf.<span class="hljs-title function_">writeUInt32BE</span>(<span class="hljs-number">0xfeedface</span>, <span class="hljs-number">0</span>);
4553
4554<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4555<span class="hljs-comment">// Prints: &#x3C;Buffer fe ed fa ce></span></code><button class="copy-button">copy</button></pre>
4556<h4><code>buf.writeUInt32LE(value[, offset])</code><span><a class="mark" href="#bufwriteuint32levalue-offset" id="bufwriteuint32levalue-offset">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeuint32le_value_offset"></a></h4>
4557<div class="api_metadata">
4558<details class="changelog"><summary>History</summary>
4559<table>
4560<tbody><tr><th>Version</th><th>Changes</th></tr>
4561<tr><td>v14.9.0, v12.19.0</td>
4562<td><p>This function is also available as <code>buf.writeUint32LE()</code>.</p></td></tr>
4563<tr><td>v10.0.0</td>
4564<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
4565<tr><td>v0.5.5</td>
4566<td><p><span>Added in: v0.5.5</span></p></td></tr>
4567</tbody></table>
4568</details>
4569</div>
4570<ul>
4571<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number to be written to <code>buf</code>.</li>
4572<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write. Must
4573satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
4574<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <code>offset</code> plus the number of bytes written.</li>
4575</ul>
4576<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code>
4577must be a valid unsigned 32-bit integer. Behavior is undefined when <code>value</code> is
4578anything other than an unsigned 32-bit integer.</p>
4579<p>This function is also available under the <code>writeUint32LE</code> alias.</p>
4580
4581<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
4582
4583<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>);
4584
4585buf.<span class="hljs-title function_">writeUInt32LE</span>(<span class="hljs-number">0xfeedface</span>, <span class="hljs-number">0</span>);
4586
4587<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4588<span class="hljs-comment">// Prints: &#x3C;Buffer ce fa ed fe></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
4589
4590<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">4</span>);
4591
4592buf.<span class="hljs-title function_">writeUInt32LE</span>(<span class="hljs-number">0xfeedface</span>, <span class="hljs-number">0</span>);
4593
4594<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4595<span class="hljs-comment">// Prints: &#x3C;Buffer ce fa ed fe></span></code><button class="copy-button">copy</button></pre>
4596<h4><code>buf.writeUIntBE(value, offset, byteLength)</code><span><a class="mark" href="#bufwriteuintbevalue-offset-bytelength" id="bufwriteuintbevalue-offset-bytelength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeuintbe_value_offset_bytelength"></a></h4>
4597<div class="api_metadata">
4598<details class="changelog"><summary>History</summary>
4599<table>
4600<tbody><tr><th>Version</th><th>Changes</th></tr>
4601<tr><td>v14.9.0, v12.19.0</td>
4602<td><p>This function is also available as <code>buf.writeUintBE()</code>.</p></td></tr>
4603<tr><td>v10.0.0</td>
4604<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset and <code>byteLength</code> to <code>uint32</code> anymore.</p></td></tr>
4605<tr><td>v0.5.5</td>
4606<td><p><span>Added in: v0.5.5</span></p></td></tr>
4607</tbody></table>
4608</details>
4609</div>
4610<ul>
4611<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number to be written to <code>buf</code>.</li>
4612<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write. Must
4613satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
4614<li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to write. Must satisfy
4615<code>0 &#x3C; byteLength &#x3C;= 6</code>.</li>
4616<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <code>offset</code> plus the number of bytes written.</li>
4617</ul>
4618<p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code>
4619as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined
4620when <code>value</code> is anything other than an unsigned integer.</p>
4621<p>This function is also available under the <code>writeUintBE</code> alias.</p>
4622
4623<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
4624
4625<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">6</span>);
4626
4627buf.<span class="hljs-title function_">writeUIntBE</span>(<span class="hljs-number">0x1234567890ab</span>, <span class="hljs-number">0</span>, <span class="hljs-number">6</span>);
4628
4629<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4630<span class="hljs-comment">// Prints: &#x3C;Buffer 12 34 56 78 90 ab></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
4631
4632<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">6</span>);
4633
4634buf.<span class="hljs-title function_">writeUIntBE</span>(<span class="hljs-number">0x1234567890ab</span>, <span class="hljs-number">0</span>, <span class="hljs-number">6</span>);
4635
4636<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4637<span class="hljs-comment">// Prints: &#x3C;Buffer 12 34 56 78 90 ab></span></code><button class="copy-button">copy</button></pre>
4638<h4><code>buf.writeUIntLE(value, offset, byteLength)</code><span><a class="mark" href="#bufwriteuintlevalue-offset-bytelength" id="bufwriteuintlevalue-offset-bytelength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buf_writeuintle_value_offset_bytelength"></a></h4>
4639<div class="api_metadata">
4640<details class="changelog"><summary>History</summary>
4641<table>
4642<tbody><tr><th>Version</th><th>Changes</th></tr>
4643<tr><td>v14.9.0, v12.19.0</td>
4644<td><p>This function is also available as <code>buf.writeUintLE()</code>.</p></td></tr>
4645<tr><td>v10.0.0</td>
4646<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset and <code>byteLength</code> to <code>uint32</code> anymore.</p></td></tr>
4647<tr><td>v0.5.5</td>
4648<td><p><span>Added in: v0.5.5</span></p></td></tr>
4649</tbody></table>
4650</details>
4651</div>
4652<ul>
4653<li><code>value</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number to be written to <code>buf</code>.</li>
4654<li><code>offset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to skip before starting to write. Must
4655satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
4656<li><code>byteLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to write. Must satisfy
4657<code>0 &#x3C; byteLength &#x3C;= 6</code>.</li>
4658<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <code>offset</code> plus the number of bytes written.</li>
4659</ul>
4660<p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code>
4661as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined
4662when <code>value</code> is anything other than an unsigned integer.</p>
4663<p>This function is also available under the <code>writeUintLE</code> alias.</p>
4664
4665<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
4666
4667<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">6</span>);
4668
4669buf.<span class="hljs-title function_">writeUIntLE</span>(<span class="hljs-number">0x1234567890ab</span>, <span class="hljs-number">0</span>, <span class="hljs-number">6</span>);
4670
4671<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4672<span class="hljs-comment">// Prints: &#x3C;Buffer ab 90 78 56 34 12></span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
4673
4674<span class="hljs-keyword">const</span> buf = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">allocUnsafe</span>(<span class="hljs-number">6</span>);
4675
4676buf.<span class="hljs-title function_">writeUIntLE</span>(<span class="hljs-number">0x1234567890ab</span>, <span class="hljs-number">0</span>, <span class="hljs-number">6</span>);
4677
4678<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
4679<span class="hljs-comment">// Prints: &#x3C;Buffer ab 90 78 56 34 12></span></code><button class="copy-button">copy</button></pre>
4680<h4><code>new Buffer(array)</code><span><a class="mark" href="#new-bufferarray" id="new-bufferarray">#</a></span><a aria-hidden="true" class="legacy" id="buffer_new_buffer_array"></a></h4>
4681<div class="api_metadata">
4682<details class="changelog"><summary>History</summary>
4683<table>
4684<tbody><tr><th>Version</th><th>Changes</th></tr>
4685<tr><td>v10.0.0</td>
4686<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr>
4687<tr><td>v7.2.1</td>
4688<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr>
4689<tr><td>v7.0.0</td>
4690<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr>
4691<tr><td>v6.0.0</td>
4692<td><p><span>Deprecated since: v6.0.0</span></p></td></tr>
4693</tbody></table>
4694</details>
4695</div>
4696<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#static-method-bufferfromarray"><code>Buffer.from(array)</code></a> instead.</div><p></p>
4697<ul>
4698<li><code>array</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer[]></a> An array of bytes to copy from.</li>
4699</ul>
4700<p>See <a href="#static-method-bufferfromarray"><code>Buffer.from(array)</code></a>.</p>
4701<h4><code>new Buffer(arrayBuffer[, byteOffset[, length]])</code><span><a class="mark" href="#new-bufferarraybuffer-byteoffset-length" id="new-bufferarraybuffer-byteoffset-length">#</a></span><a aria-hidden="true" class="legacy" id="buffer_new_buffer_arraybuffer_byteoffset_length"></a></h4>
4702<div class="api_metadata">
4703<details class="changelog"><summary>History</summary>
4704<table>
4705<tbody><tr><th>Version</th><th>Changes</th></tr>
4706<tr><td>v10.0.0</td>
4707<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr>
4708<tr><td>v7.2.1</td>
4709<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr>
4710<tr><td>v7.0.0</td>
4711<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr>
4712<tr><td>v6.0.0</td>
4713<td><p>The <code>byteOffset</code> and <code>length</code> parameters are supported now.</p></td></tr>
4714<tr><td>v6.0.0</td>
4715<td><p><span>Deprecated since: v6.0.0</span></p></td></tr>
4716<tr><td>v3.0.0</td>
4717<td><p><span>Added in: v3.0.0</span></p></td></tr>
4718</tbody></table>
4719</details>
4720</div>
4721<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use
4722<a href="#static-method-bufferfromarraybuffer-byteoffset-length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a>
4723instead.</div><p></p>
4724<ul>
4725<li><code>arrayBuffer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer" class="type">&#x3C;SharedArrayBuffer></a> An <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a>,
4726<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a> or the <code>.buffer</code> property of a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>.</li>
4727<li><code>byteOffset</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Index of first byte to expose. <strong>Default:</strong> <code>0</code>.</li>
4728<li><code>length</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> Number of bytes to expose.
4729<strong>Default:</strong> <code>arrayBuffer.byteLength - byteOffset</code>.</li>
4730</ul>
4731<p>See
4732<a href="#static-method-bufferfromarraybuffer-byteoffset-length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a>.</p>
4733<h4><code>new Buffer(buffer)</code><span><a class="mark" href="#new-bufferbuffer" id="new-bufferbuffer">#</a></span><a aria-hidden="true" class="legacy" id="buffer_new_buffer_buffer"></a></h4>
4734<div class="api_metadata">
4735<details class="changelog"><summary>History</summary>
4736<table>
4737<tbody><tr><th>Version</th><th>Changes</th></tr>
4738<tr><td>v10.0.0</td>
4739<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr>
4740<tr><td>v7.2.1</td>
4741<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr>
4742<tr><td>v7.0.0</td>
4743<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr>
4744<tr><td>v6.0.0</td>
4745<td><p><span>Deprecated since: v6.0.0</span></p></td></tr>
4746</tbody></table>
4747</details>
4748</div>
4749<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#static-method-bufferfrombuffer"><code>Buffer.from(buffer)</code></a> instead.</div><p></p>
4750<ul>
4751<li><code>buffer</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> An existing <code>Buffer</code> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array"><code>Uint8Array</code></a> from
4752which to copy data.</li>
4753</ul>
4754<p>See <a href="#static-method-bufferfrombuffer"><code>Buffer.from(buffer)</code></a>.</p>
4755<h4><code>new Buffer(size)</code><span><a class="mark" href="#new-buffersize" id="new-buffersize">#</a></span><a aria-hidden="true" class="legacy" id="buffer_new_buffer_size"></a></h4>
4756<div class="api_metadata">
4757<details class="changelog"><summary>History</summary>
4758<table>
4759<tbody><tr><th>Version</th><th>Changes</th></tr>
4760<tr><td>v10.0.0</td>
4761<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr>
4762<tr><td>v8.0.0</td>
4763<td><p>The <code>new Buffer(size)</code> will return zero-filled memory by default.</p></td></tr>
4764<tr><td>v7.2.1</td>
4765<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr>
4766<tr><td>v7.0.0</td>
4767<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr>
4768<tr><td>v6.0.0</td>
4769<td><p><span>Deprecated since: v6.0.0</span></p></td></tr>
4770</tbody></table>
4771</details>
4772</div>
4773<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc()</code></a> instead (also see
4774<a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a>).</div><p></p>
4775<ul>
4776<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The desired length of the new <code>Buffer</code>.</li>
4777</ul>
4778<p>See <a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc()</code></a> and <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a>. This variant of the
4779constructor is equivalent to <a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc()</code></a>.</p>
4780<h4><code>new Buffer(string[, encoding])</code><span><a class="mark" href="#new-bufferstring-encoding" id="new-bufferstring-encoding">#</a></span><a aria-hidden="true" class="legacy" id="buffer_new_buffer_string_encoding"></a></h4>
4781<div class="api_metadata">
4782<details class="changelog"><summary>History</summary>
4783<table>
4784<tbody><tr><th>Version</th><th>Changes</th></tr>
4785<tr><td>v10.0.0</td>
4786<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr>
4787<tr><td>v7.2.1</td>
4788<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr>
4789<tr><td>v7.0.0</td>
4790<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr>
4791<tr><td>v6.0.0</td>
4792<td><p><span>Deprecated since: v6.0.0</span></p></td></tr>
4793</tbody></table>
4794</details>
4795</div>
4796<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated:
4797Use <a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string[, encoding])</code></a> instead.</div><p></p>
4798<ul>
4799<li><code>string</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> String to encode.</li>
4800<li><code>encoding</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The encoding of <code>string</code>. <strong>Default:</strong> <code>'utf8'</code>.</li>
4801</ul>
4802<p>See <a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string[, encoding])</code></a>.</p>
4803</section><section><h3>Class: <code>File</code><span><a class="mark" href="#class-file" id="class-file">#</a></span><a aria-hidden="true" class="legacy" id="buffer_class_file"></a></h3>
4804<div class="api_metadata">
4805<span>Added in: v18.13.0</span>
4806</div>
4807<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
4808<ul>
4809<li>Extends: <a href="buffer.html#class-blob" class="type">&#x3C;Blob></a></li>
4810</ul>
4811<p>A <a href="https://developer.mozilla.org/en-US/docs/Web/API/File"><code>File</code></a> provides information about files.</p>
4812<h4><code>new buffer.File(sources, fileName[, options])</code><span><a class="mark" href="#new-bufferfilesources-filename-options" id="new-bufferfilesources-filename-options">#</a></span><a aria-hidden="true" class="legacy" id="buffer_new_buffer_file_sources_filename_options"></a></h4>
4813<div class="api_metadata">
4814<span>Added in: v18.13.0</span>
4815</div>
4816<ul>
4817<li><code>sources</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray[]></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView[]></a> | <a href="buffer.html#class-blob" class="type">&#x3C;Blob[]></a> | <a href="buffer.html#class-file" class="type">&#x3C;File[]></a>
4818An array of string, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView" class="type">&#x3C;DataView></a>, <a href="buffer.html#class-file" class="type">&#x3C;File></a>, or <a href="buffer.html#class-blob" class="type">&#x3C;Blob></a>
4819objects, or any mix of such objects, that will be stored within the <code>File</code>.</li>
4820<li><code>fileName</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The name of the file.</li>
4821<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
4822<ul>
4823<li><code>endings</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> One of either <code>'transparent'</code> or <code>'native'</code>. When set
4824to <code>'native'</code>, line endings in string source parts will be converted to
4825the platform native line-ending as specified by <code>require('node:os').EOL</code>.</li>
4826<li><code>type</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The File content-type.</li>
4827<li><code>lastModified</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The last modified date of the file.
4828<strong>Default:</strong> <code>Date.now()</code>.</li>
4829</ul>
4830</li>
4831</ul>
4832<h4><code>file.name</code><span><a class="mark" href="#filename" id="filename">#</a></span><a aria-hidden="true" class="legacy" id="buffer_file_name"></a></h4>
4833<div class="api_metadata">
4834<span>Added in: v18.13.0</span>
4835</div>
4836<ul>
4837<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
4838</ul>
4839<p>The name of the <code>File</code>.</p>
4840<h4><code>file.lastModified</code><span><a class="mark" href="#filelastmodified" id="filelastmodified">#</a></span><a aria-hidden="true" class="legacy" id="buffer_file_lastmodified"></a></h4>
4841<div class="api_metadata">
4842<span>Added in: v18.13.0</span>
4843</div>
4844<ul>
4845<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
4846</ul>
4847<p>The last modified date of the <code>File</code>.</p>
4848</section><section><h3><code>node:buffer</code> module APIs<span><a class="mark" href="#nodebuffer-module-apis" id="nodebuffer-module-apis">#</a></span><a aria-hidden="true" class="legacy" id="buffer_node_buffer_module_apis"></a></h3>
4849<p>While, the <code>Buffer</code> object is available as a global, there are additional
4850<code>Buffer</code>-related APIs that are available only via the <code>node:buffer</code> module
4851accessed using <code>require('node:buffer')</code>.</p>
4852<h4><code>buffer.atob(data)</code><span><a class="mark" href="#bufferatobdata" id="bufferatobdata">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_atob_data"></a></h4>
4853<div class="api_metadata">
4854<span>Added in: v15.13.0, v14.17.0</span>
4855</div>
4856<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy. Use <code>Buffer.from(data, 'base64')</code> instead.</div><p></p>
4857<ul>
4858<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> The Base64-encoded input string.</li>
4859</ul>
4860<p>Decodes a string of Base64-encoded data into bytes, and encodes those bytes
4861into a string using Latin-1 (ISO-8859-1).</p>
4862<p>The <code>data</code> may be any JavaScript-value that can be coerced into a string.</p>
4863<p><strong>This function is only provided for compatibility with legacy web platform APIs
4864and should never be used in new code, because they use strings to represent
4865binary data and predate the introduction of typed arrays in JavaScript.
4866For code running using Node.js APIs, converting between base64-encoded strings
4867and binary data should be performed using <code>Buffer.from(str, 'base64')</code> and
4868<code>buf.toString('base64')</code>.</strong></p>
4869<h4><code>buffer.btoa(data)</code><span><a class="mark" href="#bufferbtoadata" id="bufferbtoadata">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_btoa_data"></a></h4>
4870<div class="api_metadata">
4871<span>Added in: v15.13.0, v14.17.0</span>
4872</div>
4873<p></p><div class="api_stability api_stability_3"><a href="documentation.html#stability-index">Stability: 3</a> - Legacy. Use <code>buf.toString('base64')</code> instead.</div><p></p>
4874<ul>
4875<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> An ASCII (Latin1) string.</li>
4876</ul>
4877<p>Decodes a string into bytes using Latin-1 (ISO-8859), and encodes those bytes
4878into a string using Base64.</p>
4879<p>The <code>data</code> may be any JavaScript-value that can be coerced into a string.</p>
4880<p><strong>This function is only provided for compatibility with legacy web platform APIs
4881and should never be used in new code, because they use strings to represent
4882binary data and predate the introduction of typed arrays in JavaScript.
4883For code running using Node.js APIs, converting between base64-encoded strings
4884and binary data should be performed using <code>Buffer.from(str, 'base64')</code> and
4885<code>buf.toString('base64')</code>.</strong></p>
4886<h4><code>buffer.isAscii(input)</code><span><a class="mark" href="#bufferisasciiinput" id="bufferisasciiinput">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_isascii_input"></a></h4>
4887<div class="api_metadata">
4888<span>Added in: v18.15.0</span>
4889</div>
4890<ul>
4891<li>input <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> The input to validate.</li>
4892<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
4893</ul>
4894<p>This function returns <code>true</code> if <code>input</code> contains only valid ASCII-encoded data,
4895including the case in which <code>input</code> is empty.</p>
4896<p>Throws if the <code>input</code> is a detached array buffer.</p>
4897<h4><code>buffer.isUtf8(input)</code><span><a class="mark" href="#bufferisutf8input" id="bufferisutf8input">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_isutf8_input"></a></h4>
4898<div class="api_metadata">
4899<span>Added in: v18.14.0</span>
4900</div>
4901<ul>
4902<li>input <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer" class="type">&#x3C;ArrayBuffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray" class="type">&#x3C;TypedArray></a> The input to validate.</li>
4903<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
4904</ul>
4905<p>This function returns <code>true</code> if <code>input</code> contains only valid UTF-8-encoded data,
4906including the case in which <code>input</code> is empty.</p>
4907<p>Throws if the <code>input</code> is a detached array buffer.</p>
4908<h4><code>buffer.INSPECT_MAX_BYTES</code><span><a class="mark" href="#bufferinspect_max_bytes" id="bufferinspect_max_bytes">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_inspect_max_bytes"></a></h4>
4909<div class="api_metadata">
4910<span>Added in: v0.5.4</span>
4911</div>
4912<ul>
4913<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> <strong>Default:</strong> <code>50</code></li>
4914</ul>
4915<p>Returns the maximum number of bytes that will be returned when
4916<code>buf.inspect()</code> is called. This can be overridden by user modules. See
4917<a href="util.html#utilinspectobject-options"><code>util.inspect()</code></a> for more details on <code>buf.inspect()</code> behavior.</p>
4918<h4><code>buffer.kMaxLength</code><span><a class="mark" href="#bufferkmaxlength" id="bufferkmaxlength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_kmaxlength"></a></h4>
4919<div class="api_metadata">
4920<span>Added in: v3.0.0</span>
4921</div>
4922<ul>
4923<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The largest size allowed for a single <code>Buffer</code> instance.</li>
4924</ul>
4925<p>An alias for <a href="#bufferconstantsmax_length"><code>buffer.constants.MAX_LENGTH</code></a>.</p>
4926<h4><code>buffer.kStringMaxLength</code><span><a class="mark" href="#bufferkstringmaxlength" id="bufferkstringmaxlength">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_kstringmaxlength"></a></h4>
4927<div class="api_metadata">
4928<span>Added in: v3.0.0</span>
4929</div>
4930<ul>
4931<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The largest length allowed for a single <code>string</code> instance.</li>
4932</ul>
4933<p>An alias for <a href="#bufferconstantsmax_string_length"><code>buffer.constants.MAX_STRING_LENGTH</code></a>.</p>
4934<h4><code>buffer.resolveObjectURL(id)</code><span><a class="mark" href="#bufferresolveobjecturlid" id="bufferresolveobjecturlid">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_resolveobjecturl_id"></a></h4>
4935<div class="api_metadata">
4936<span>Added in: v16.7.0</span>
4937</div>
4938<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p>
4939<ul>
4940<li><code>id</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> A <code>'blob:nodedata:...</code> URL string returned by a prior call to
4941<code>URL.createObjectURL()</code>.</li>
4942<li>Returns: <a href="buffer.html#class-blob" class="type">&#x3C;Blob></a></li>
4943</ul>
4944<p>Resolves a <code>'blob:nodedata:...'</code> an associated <a href="buffer.html#class-blob" class="type">&#x3C;Blob></a> object registered using
4945a prior call to <code>URL.createObjectURL()</code>.</p>
4946<h4><code>buffer.transcode(source, fromEnc, toEnc)</code><span><a class="mark" href="#buffertranscodesource-fromenc-toenc" id="buffertranscodesource-fromenc-toenc">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_transcode_source_fromenc_toenc"></a></h4>
4947<div class="api_metadata">
4948<details class="changelog"><summary>History</summary>
4949<table>
4950<tbody><tr><th>Version</th><th>Changes</th></tr>
4951<tr><td>v8.0.0</td>
4952<td><p>The <code>source</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
4953<tr><td>v7.1.0</td>
4954<td><p><span>Added in: v7.1.0</span></p></td></tr>
4955</tbody></table>
4956</details>
4957</div>
4958<ul>
4959<li><code>source</code> <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array" class="type">&#x3C;Uint8Array></a> A <code>Buffer</code> or <code>Uint8Array</code> instance.</li>
4960<li><code>fromEnc</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The current encoding.</li>
4961<li><code>toEnc</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> To target encoding.</li>
4962<li>Returns: <a href="buffer.html#class-buffer" class="type">&#x3C;Buffer></a></li>
4963</ul>
4964<p>Re-encodes the given <code>Buffer</code> or <code>Uint8Array</code> instance from one character
4965encoding to another. Returns a new <code>Buffer</code> instance.</p>
4966<p>Throws if the <code>fromEnc</code> or <code>toEnc</code> specify invalid character encodings or if
4967conversion from <code>fromEnc</code> to <code>toEnc</code> is not permitted.</p>
4968<p>Encodings supported by <code>buffer.transcode()</code> are: <code>'ascii'</code>, <code>'utf8'</code>,
4969<code>'utf16le'</code>, <code>'ucs2'</code>, <code>'latin1'</code>, and <code>'binary'</code>.</p>
4970<p>The transcoding process will use substitution characters if a given byte
4971sequence cannot be adequately represented in the target encoding. For instance:</p>
4972
4973<pre class="with-53-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> { <span class="hljs-title class_">Buffer</span>, transcode } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:buffer'</span>;
4974
4975<span class="hljs-keyword">const</span> newBuf = <span class="hljs-title function_">transcode</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'€'</span>), <span class="hljs-string">'utf8'</span>, <span class="hljs-string">'ascii'</span>);
4976<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(newBuf.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'ascii'</span>));
4977<span class="hljs-comment">// Prints: '?'</span></code><code class="language-js cjs"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Buffer</span>, transcode } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:buffer'</span>);
4978
4979<span class="hljs-keyword">const</span> newBuf = <span class="hljs-title function_">transcode</span>(<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'€'</span>), <span class="hljs-string">'utf8'</span>, <span class="hljs-string">'ascii'</span>);
4980<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(newBuf.<span class="hljs-title function_">toString</span>(<span class="hljs-string">'ascii'</span>));
4981<span class="hljs-comment">// Prints: '?'</span></code><button class="copy-button">copy</button></pre>
4982<p>Because the Euro (<code>€</code>) sign is not representable in US-ASCII, it is replaced
4983with <code>?</code> in the transcoded <code>Buffer</code>.</p>
4984<h4>Class: <code>SlowBuffer</code><span><a class="mark" href="#class-slowbuffer" id="class-slowbuffer">#</a></span><a aria-hidden="true" class="legacy" id="buffer_class_slowbuffer"></a></h4>
4985<div class="api_metadata">
4986<span>Deprecated since: v6.0.0</span>
4987</div>
4988<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow()</code></a> instead.</div><p></p>
4989<p>See <a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow()</code></a>. This was never a class in the sense that
4990the constructor always returned a <code>Buffer</code> instance, rather than a <code>SlowBuffer</code>
4991instance.</p>
4992<h5><code>new SlowBuffer(size)</code><span><a class="mark" href="#new-slowbuffersize" id="new-slowbuffersize">#</a></span><a aria-hidden="true" class="legacy" id="buffer_new_slowbuffer_size"></a></h5>
4993<div class="api_metadata">
4994<span>Deprecated since: v6.0.0</span>
4995</div>
4996<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated: Use <a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow()</code></a> instead.</div><p></p>
4997<ul>
4998<li><code>size</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The desired length of the new <code>SlowBuffer</code>.</li>
4999</ul>
5000<p>See <a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow()</code></a>.</p>
5001<h4>Buffer constants<span><a class="mark" href="#buffer-constants" id="buffer-constants">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_constants"></a></h4>
5002<div class="api_metadata">
5003<span>Added in: v8.2.0</span>
5004</div>
5005<h5><code>buffer.constants.MAX_LENGTH</code><span><a class="mark" href="#bufferconstantsmax_length" id="bufferconstantsmax_length">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_constants_max_length"></a></h5>
5006<div class="api_metadata">
5007<details class="changelog"><summary>History</summary>
5008<table>
5009<tbody><tr><th>Version</th><th>Changes</th></tr>
5010<tr><td>v15.0.0</td>
5011<td><p>Value is changed to 2<sup>32</sup> on 64-bit architectures.</p></td></tr>
5012<tr><td>v14.0.0</td>
5013<td><p>Value is changed from 2<sup>31</sup> - 1 to 2<sup>32</sup> - 1 on 64-bit architectures.</p></td></tr>
5014<tr><td>v8.2.0</td>
5015<td><p><span>Added in: v8.2.0</span></p></td></tr>
5016</tbody></table>
5017</details>
5018</div>
5019<ul>
5020<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The largest size allowed for a single <code>Buffer</code> instance.</li>
5021</ul>
5022<p>On 32-bit architectures, this value currently is 2<sup>30</sup> - 1 (about 1
5023GiB).</p>
5024<p>On 64-bit architectures, this value currently is 2<sup>32</sup> (about 4 GiB).</p>
5025<p>It reflects <a href="https://v8.github.io/api/head/classv8_1_1TypedArray.html#a54a48f4373da0850663c4393d843b9b0"><code>v8::TypedArray::kMaxLength</code></a> under the hood.</p>
5026<p>This value is also available as <a href="#bufferkmaxlength"><code>buffer.kMaxLength</code></a>.</p>
5027<h5><code>buffer.constants.MAX_STRING_LENGTH</code><span><a class="mark" href="#bufferconstantsmax_string_length" id="bufferconstantsmax_string_length">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_constants_max_string_length"></a></h5>
5028<div class="api_metadata">
5029<span>Added in: v8.2.0</span>
5030</div>
5031<ul>
5032<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The largest length allowed for a single <code>string</code> instance.</li>
5033</ul>
5034<p>Represents the largest <code>length</code> that a <code>string</code> primitive can have, counted
5035in UTF-16 code units.</p>
5036<p>This value may depend on the JS engine that is being used.</p>
5037</section><section><h3><code>Buffer.from()</code>, <code>Buffer.alloc()</code>, and <code>Buffer.allocUnsafe()</code><span><a class="mark" href="#bufferfrom-bufferalloc-and-bufferallocunsafe" id="bufferfrom-bufferalloc-and-bufferallocunsafe">#</a></span><a aria-hidden="true" class="legacy" id="buffer_buffer_from_buffer_alloc_and_buffer_allocunsafe"></a></h3>
5038<p>In versions of Node.js prior to 6.0.0, <code>Buffer</code> instances were created using the
5039<code>Buffer</code> constructor function, which allocates the returned <code>Buffer</code>
5040differently based on what arguments are provided:</p>
5041<ul>
5042<li>Passing a number as the first argument to <code>Buffer()</code> (e.g. <code>new Buffer(10)</code>)
5043allocates a new <code>Buffer</code> object of the specified size. Prior to Node.js 8.0.0,
5044the memory allocated for such <code>Buffer</code> instances is <em>not</em> initialized and
5045<em>can contain sensitive data</em>. Such <code>Buffer</code> instances <em>must</em> be subsequently
5046initialized by using either <a href="#buffillvalue-offset-end-encoding"><code>buf.fill(0)</code></a> or by writing to the
5047entire <code>Buffer</code> before reading data from the <code>Buffer</code>.
5048While this behavior is <em>intentional</em> to improve performance,
5049development experience has demonstrated that a more explicit distinction is
5050required between creating a fast-but-uninitialized <code>Buffer</code> versus creating a
5051slower-but-safer <code>Buffer</code>. Since Node.js 8.0.0, <code>Buffer(num)</code> and <code>new Buffer(num)</code> return a <code>Buffer</code> with initialized memory.</li>
5052<li>Passing a string, array, or <code>Buffer</code> as the first argument copies the
5053passed object's data into the <code>Buffer</code>.</li>
5054<li>Passing an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a> or a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer"><code>SharedArrayBuffer</code></a> returns a <code>Buffer</code>
5055that shares allocated memory with the given array buffer.</li>
5056</ul>
5057<p>Because the behavior of <code>new Buffer()</code> is different depending on the type of the
5058first argument, security and reliability issues can be inadvertently introduced
5059into applications when argument validation or <code>Buffer</code> initialization is not
5060performed.</p>
5061<p>For example, if an attacker can cause an application to receive a number where
5062a string is expected, the application may call <code>new Buffer(100)</code>
5063instead of <code>new Buffer("100")</code>, leading it to allocate a 100 byte buffer instead
5064of allocating a 3 byte buffer with content <code>"100"</code>. This is commonly possible
5065using JSON API calls. Since JSON distinguishes between numeric and string types,
5066it allows injection of numbers where a naively written application that does not
5067validate its input sufficiently might expect to always receive a string.
5068Before Node.js 8.0.0, the 100 byte buffer might contain
5069arbitrary pre-existing in-memory data, so may be used to expose in-memory
5070secrets to a remote attacker. Since Node.js 8.0.0, exposure of memory cannot
5071occur because the data is zero-filled. However, other attacks are still
5072possible, such as causing very large buffers to be allocated by the server,
5073leading to performance degradation or crashing on memory exhaustion.</p>
5074<p>To make the creation of <code>Buffer</code> instances more reliable and less error-prone,
5075the various forms of the <code>new Buffer()</code> constructor have been <strong>deprecated</strong>
5076and replaced by separate <code>Buffer.from()</code>, <a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc()</code></a>, and
5077<a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> methods.</p>
5078<p><em>Developers should migrate all existing uses of the <code>new Buffer()</code> constructors
5079to one of these new APIs.</em></p>
5080<ul>
5081<li><a href="#static-method-bufferfromarray"><code>Buffer.from(array)</code></a> returns a new <code>Buffer</code> that <em>contains a copy</em> of the
5082provided octets.</li>
5083<li><a href="#static-method-bufferfromarraybuffer-byteoffset-length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a>
5084returns a new <code>Buffer</code> that <em>shares the same allocated memory</em> as the given
5085<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a>.</li>
5086<li><a href="#static-method-bufferfrombuffer"><code>Buffer.from(buffer)</code></a> returns a new <code>Buffer</code> that <em>contains a copy</em> of the
5087contents of the given <code>Buffer</code>.</li>
5088<li><a href="#static-method-bufferfromstring-encoding"><code>Buffer.from(string[, encoding])</code></a> returns a new
5089<code>Buffer</code> that <em>contains a copy</em> of the provided string.</li>
5090<li><a href="#static-method-bufferallocsize-fill-encoding"><code>Buffer.alloc(size[, fill[, encoding]])</code></a> returns a new
5091initialized <code>Buffer</code> of the specified size. This method is slower than
5092<a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe(size)</code></a> but guarantees that newly
5093created <code>Buffer</code> instances never contain old data that is potentially
5094sensitive. A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</li>
5095<li><a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe(size)</code></a> and
5096<a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow(size)</code></a> each return a
5097new uninitialized <code>Buffer</code> of the specified <code>size</code>. Because the <code>Buffer</code> is
5098uninitialized, the allocated segment of memory might contain old data that is
5099potentially sensitive.</li>
5100</ul>
5101<p><code>Buffer</code> instances returned by <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> and
5102<a href="#static-method-bufferfromarray"><code>Buffer.from(array)</code></a> <em>may</em> be allocated off a shared internal memory pool
5103if <code>size</code> is less than or equal to half <a href="#class-property-bufferpoolsize"><code>Buffer.poolSize</code></a>. Instances
5104returned by <a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow()</code></a> <em>never</em> use the shared internal
5105memory pool.</p>
5106<h4>The <code>--zero-fill-buffers</code> command-line option<span><a class="mark" href="#the---zero-fill-buffers-command-line-option" id="the---zero-fill-buffers-command-line-option">#</a></span><a aria-hidden="true" class="legacy" id="buffer_the_zero_fill_buffers_command_line_option"></a></h4>
5107<div class="api_metadata">
5108<span>Added in: v5.10.0</span>
5109</div>
5110<p>Node.js can be started using the <code>--zero-fill-buffers</code> command-line option to
5111cause all newly-allocated <code>Buffer</code> instances to be zero-filled upon creation by
5112default. Without the option, buffers created with <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a>,
5113<a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow()</code></a>, and <code>new SlowBuffer(size)</code> are not zero-filled.
5114Use of this flag can have a measurable negative impact on performance. Use the
5115<code>--zero-fill-buffers</code> option only when necessary to enforce that newly allocated
5116<code>Buffer</code> instances cannot contain old data that is potentially sensitive.</p>
5117<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node --zero-fill-buffers</span>
5118<span class="hljs-meta prompt_">> </span><span class="language-bash">Buffer.allocUnsafe(5);</span>
5119&#x3C;Buffer 00 00 00 00 00></code> <button class="copy-button">copy</button></pre>
5120<h4>What makes <code>Buffer.allocUnsafe()</code> and <code>Buffer.allocUnsafeSlow()</code> "unsafe"?<span><a class="mark" href="#what-makes-bufferallocunsafe-and-bufferallocunsafeslow-unsafe" id="what-makes-bufferallocunsafe-and-bufferallocunsafeslow-unsafe">#</a></span><a aria-hidden="true" class="legacy" id="buffer_what_makes_buffer_allocunsafe_and_buffer_allocunsafeslow_unsafe"></a></h4>
5121<p>When calling <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> and <a href="#static-method-bufferallocunsafeslowsize"><code>Buffer.allocUnsafeSlow()</code></a>, the
5122segment of allocated memory is <em>uninitialized</em> (it is not zeroed-out). While
5123this design makes the allocation of memory quite fast, the allocated segment of
5124memory might contain old data that is potentially sensitive. Using a <code>Buffer</code>
5125created by <a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a> without <em>completely</em> overwriting the
5126memory can allow this old data to be leaked when the <code>Buffer</code> memory is read.</p>
5127<p>While there are clear performance advantages to using
5128<a href="#static-method-bufferallocunsafesize"><code>Buffer.allocUnsafe()</code></a>, extra care <em>must</em> be taken in order to avoid
5129introducing security vulnerabilities into an application.</p></section>
5130        <!-- API END -->
5131      </div>
5132    </div>
5133  </div>
5134</body>
5135</html>
5136