• 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="v14.19.1">
7  <title>Buffer | Node.js v14.19.1 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</head>
13<body class="alt apidoc" id="api-section-buffer">
14  <div id="content" class="clearfix">
15    <div id="column2" class="interior">
16      <div id="intro" class="interior">
17        <a href="/" title="Go back to the home page">
18          Node.js
19        </a>
20      </div>
21      <ul>
22<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
23<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
24</ul>
25<hr class="line">
26<ul>
27<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
28<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
29<li><a href="buffer.html" class="nav-buffer active">Buffer</a></li>
30<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
31<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
32<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
33<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
34<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
35<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
36<li><a href="console.html" class="nav-console">Console</a></li>
37<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
38<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
39<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
40<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
41<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
42<li><a href="dns.html" class="nav-dns">DNS</a></li>
43<li><a href="domain.html" class="nav-domain">Domain</a></li>
44<li><a href="errors.html" class="nav-errors">Errors</a></li>
45<li><a href="events.html" class="nav-events">Events</a></li>
46<li><a href="fs.html" class="nav-fs">File system</a></li>
47<li><a href="globals.html" class="nav-globals">Globals</a></li>
48<li><a href="http.html" class="nav-http">HTTP</a></li>
49<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
50<li><a href="https.html" class="nav-https">HTTPS</a></li>
51<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
52<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
53<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
54<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
55<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li>
56<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
57<li><a href="net.html" class="nav-net">Net</a></li>
58<li><a href="os.html" class="nav-os">OS</a></li>
59<li><a href="path.html" class="nav-path">Path</a></li>
60<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
61<li><a href="policy.html" class="nav-policy">Policies</a></li>
62<li><a href="process.html" class="nav-process">Process</a></li>
63<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
64<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
65<li><a href="readline.html" class="nav-readline">Readline</a></li>
66<li><a href="repl.html" class="nav-repl">REPL</a></li>
67<li><a href="report.html" class="nav-report">Report</a></li>
68<li><a href="stream.html" class="nav-stream">Stream</a></li>
69<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
70<li><a href="timers.html" class="nav-timers">Timers</a></li>
71<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
72<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
73<li><a href="tty.html" class="nav-tty">TTY</a></li>
74<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
75<li><a href="url.html" class="nav-url">URL</a></li>
76<li><a href="util.html" class="nav-util">Utilities</a></li>
77<li><a href="v8.html" class="nav-v8">V8</a></li>
78<li><a href="vm.html" class="nav-vm">VM</a></li>
79<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
80<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
81<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
82</ul>
83<hr class="line">
84<ul>
85<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
86</ul>
87    </div>
88
89    <div id="column1" data-id="buffer" class="interior">
90      <header>
91        <div class="header-container">
92          <h1>Node.js v14.19.1 documentation</h1>
93          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
94            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
95              <path fill="none" d="M0 0h24v24H0z" />
96              <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"/>
97              <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"/>
98            </svg>
99            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
100              <path d="M0 0h24v24H0z" fill="none" />
101              <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"/>
102            </svg>
103          </button>
104        </div>
105        <div id="gtoc">
106          <ul>
107            <li>
108              <a href="index.html">Index</a>
109            </li>
110            <li>
111              <a href="all.html">View on single page</a>
112            </li>
113            <li>
114              <a href="buffer.json">View as JSON</a>
115            </li>
116
117    <li class="version-picker">
118      <a href="#">View another version <span>&#x25bc;</span></a>
119      <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v17.x/api/buffer.html">17.x</a></li>
120<li><a href="https://nodejs.org/docs/latest-v16.x/api/buffer.html">16.x <b>LTS</b></a></li>
121<li><a href="https://nodejs.org/docs/latest-v15.x/api/buffer.html">15.x</a></li>
122<li><a href="https://nodejs.org/docs/latest-v14.x/api/buffer.html">14.x <b>LTS</b></a></li>
123<li><a href="https://nodejs.org/docs/latest-v13.x/api/buffer.html">13.x</a></li>
124<li><a href="https://nodejs.org/docs/latest-v12.x/api/buffer.html">12.x <b>LTS</b></a></li>
125<li><a href="https://nodejs.org/docs/latest-v11.x/api/buffer.html">11.x</a></li>
126<li><a href="https://nodejs.org/docs/latest-v10.x/api/buffer.html">10.x</a></li>
127<li><a href="https://nodejs.org/docs/latest-v9.x/api/buffer.html">9.x</a></li>
128<li><a href="https://nodejs.org/docs/latest-v8.x/api/buffer.html">8.x</a></li>
129<li><a href="https://nodejs.org/docs/latest-v7.x/api/buffer.html">7.x</a></li>
130<li><a href="https://nodejs.org/docs/latest-v6.x/api/buffer.html">6.x</a></li>
131<li><a href="https://nodejs.org/docs/latest-v5.x/api/buffer.html">5.x</a></li>
132<li><a href="https://nodejs.org/docs/latest-v4.x/api/buffer.html">4.x</a></li>
133<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/buffer.html">0.12.x</a></li>
134<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/buffer.html">0.10.x</a></li></ol>
135    </li>
136
137            <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/buffer.md">Edit on GitHub</a></li>
138          </ul>
139        </div>
140        <hr>
141      </header>
142
143      <details id="toc" open><summary>Table of contents</summary><ul>
144<li><span class="stability_2"><a href="#buffer_buffer">Buffer</a></span>
145<ul>
146<li><a href="#buffer_buffers_and_character_encodings">Buffers and character encodings</a></li>
147<li><a href="#buffer_buffers_and_typedarrays">Buffers and TypedArrays</a></li>
148<li><a href="#buffer_buffers_and_iteration">Buffers and iteration</a></li>
149<li><span class="stability_1"><a href="#buffer_class_blob">Class: <code>Blob</code></a></span>
150<ul>
151<li><a href="#buffer_new_buffer_blob_sources_options"><code>new buffer.Blob([sources[, options]])</code></a></li>
152<li><a href="#buffer_blob_arraybuffer"><code>blob.arrayBuffer()</code></a></li>
153<li><a href="#buffer_blob_size"><code>blob.size</code></a></li>
154<li><a href="#buffer_blob_slice_start_end_type"><code>blob.slice([start, [end, [type]]])</code></a></li>
155<li><a href="#buffer_blob_text"><code>blob.text()</code></a></li>
156<li><a href="#buffer_blob_type"><code>blob.type</code></a></li>
157<li><a href="#buffer_blob_objects_and_messagechannel"><code>Blob</code> objects and <code>MessageChannel</code></a></li>
158</ul>
159</li>
160<li><a href="#buffer_class_buffer">Class: <code>Buffer</code></a>
161<ul>
162<li><a href="#buffer_static_method_buffer_alloc_size_fill_encoding">Static method: <code>Buffer.alloc(size[, fill[, encoding]])</code></a></li>
163<li><a href="#buffer_static_method_buffer_allocunsafe_size">Static method: <code>Buffer.allocUnsafe(size)</code></a></li>
164<li><a href="#buffer_static_method_buffer_allocunsafeslow_size">Static method: <code>Buffer.allocUnsafeSlow(size)</code></a></li>
165<li><a href="#buffer_static_method_buffer_bytelength_string_encoding">Static method: <code>Buffer.byteLength(string[, encoding])</code></a></li>
166<li><a href="#buffer_static_method_buffer_compare_buf1_buf2">Static method: <code>Buffer.compare(buf1, buf2)</code></a></li>
167<li><a href="#buffer_static_method_buffer_concat_list_totallength">Static method: <code>Buffer.concat(list[, totalLength])</code></a></li>
168<li><a href="#buffer_static_method_buffer_from_array">Static method: <code>Buffer.from(array)</code></a></li>
169<li><a href="#buffer_static_method_buffer_from_arraybuffer_byteoffset_length">Static method: <code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a></li>
170<li><a href="#buffer_static_method_buffer_from_buffer">Static method: <code>Buffer.from(buffer)</code></a></li>
171<li><a href="#buffer_static_method_buffer_from_object_offsetorencoding_length">Static method: <code>Buffer.from(object[, offsetOrEncoding[, length]])</code></a></li>
172<li><a href="#buffer_static_method_buffer_from_string_encoding">Static method: <code>Buffer.from(string[, encoding])</code></a></li>
173<li><a href="#buffer_static_method_buffer_isbuffer_obj">Static method: <code>Buffer.isBuffer(obj)</code></a></li>
174<li><a href="#buffer_static_method_buffer_isencoding_encoding">Static method: <code>Buffer.isEncoding(encoding)</code></a></li>
175<li><a href="#buffer_class_property_buffer_poolsize">Class property: <code>Buffer.poolSize</code></a></li>
176<li><a href="#buffer_buf_index"><code>buf[index]</code></a></li>
177<li><a href="#buffer_buf_buffer"><code>buf.buffer</code></a></li>
178<li><a href="#buffer_buf_byteoffset"><code>buf.byteOffset</code></a></li>
179<li><a href="#buffer_buf_compare_target_targetstart_targetend_sourcestart_sourceend"><code>buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])</code></a></li>
180<li><a href="#buffer_buf_copy_target_targetstart_sourcestart_sourceend"><code>buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])</code></a></li>
181<li><a href="#buffer_buf_entries"><code>buf.entries()</code></a></li>
182<li><a href="#buffer_buf_equals_otherbuffer"><code>buf.equals(otherBuffer)</code></a></li>
183<li><a href="#buffer_buf_fill_value_offset_end_encoding"><code>buf.fill(value[, offset[, end]][, encoding])</code></a></li>
184<li><a href="#buffer_buf_includes_value_byteoffset_encoding"><code>buf.includes(value[, byteOffset][, encoding])</code></a></li>
185<li><a href="#buffer_buf_indexof_value_byteoffset_encoding"><code>buf.indexOf(value[, byteOffset][, encoding])</code></a></li>
186<li><a href="#buffer_buf_keys"><code>buf.keys()</code></a></li>
187<li><a href="#buffer_buf_lastindexof_value_byteoffset_encoding"><code>buf.lastIndexOf(value[, byteOffset][, encoding])</code></a></li>
188<li><a href="#buffer_buf_length"><code>buf.length</code></a></li>
189<li><span class="stability_0"><a href="#buffer_buf_parent"><code>buf.parent</code></a></span></li>
190<li><a href="#buffer_buf_readbigint64be_offset"><code>buf.readBigInt64BE([offset])</code></a></li>
191<li><a href="#buffer_buf_readbigint64le_offset"><code>buf.readBigInt64LE([offset])</code></a></li>
192<li><a href="#buffer_buf_readbiguint64be_offset"><code>buf.readBigUInt64BE([offset])</code></a></li>
193<li><a href="#buffer_buf_readbiguint64le_offset"><code>buf.readBigUInt64LE([offset])</code></a></li>
194<li><a href="#buffer_buf_readdoublebe_offset"><code>buf.readDoubleBE([offset])</code></a></li>
195<li><a href="#buffer_buf_readdoublele_offset"><code>buf.readDoubleLE([offset])</code></a></li>
196<li><a href="#buffer_buf_readfloatbe_offset"><code>buf.readFloatBE([offset])</code></a></li>
197<li><a href="#buffer_buf_readfloatle_offset"><code>buf.readFloatLE([offset])</code></a></li>
198<li><a href="#buffer_buf_readint8_offset"><code>buf.readInt8([offset])</code></a></li>
199<li><a href="#buffer_buf_readint16be_offset"><code>buf.readInt16BE([offset])</code></a></li>
200<li><a href="#buffer_buf_readint16le_offset"><code>buf.readInt16LE([offset])</code></a></li>
201<li><a href="#buffer_buf_readint32be_offset"><code>buf.readInt32BE([offset])</code></a></li>
202<li><a href="#buffer_buf_readint32le_offset"><code>buf.readInt32LE([offset])</code></a></li>
203<li><a href="#buffer_buf_readintbe_offset_bytelength"><code>buf.readIntBE(offset, byteLength)</code></a></li>
204<li><a href="#buffer_buf_readintle_offset_bytelength"><code>buf.readIntLE(offset, byteLength)</code></a></li>
205<li><a href="#buffer_buf_readuint8_offset"><code>buf.readUInt8([offset])</code></a></li>
206<li><a href="#buffer_buf_readuint16be_offset"><code>buf.readUInt16BE([offset])</code></a></li>
207<li><a href="#buffer_buf_readuint16le_offset"><code>buf.readUInt16LE([offset])</code></a></li>
208<li><a href="#buffer_buf_readuint32be_offset"><code>buf.readUInt32BE([offset])</code></a></li>
209<li><a href="#buffer_buf_readuint32le_offset"><code>buf.readUInt32LE([offset])</code></a></li>
210<li><a href="#buffer_buf_readuintbe_offset_bytelength"><code>buf.readUIntBE(offset, byteLength)</code></a></li>
211<li><a href="#buffer_buf_readuintle_offset_bytelength"><code>buf.readUIntLE(offset, byteLength)</code></a></li>
212<li><a href="#buffer_buf_subarray_start_end"><code>buf.subarray([start[, end]])</code></a></li>
213<li><a href="#buffer_buf_slice_start_end"><code>buf.slice([start[, end]])</code></a></li>
214<li><a href="#buffer_buf_swap16"><code>buf.swap16()</code></a></li>
215<li><a href="#buffer_buf_swap32"><code>buf.swap32()</code></a></li>
216<li><a href="#buffer_buf_swap64"><code>buf.swap64()</code></a></li>
217<li><a href="#buffer_buf_tojson"><code>buf.toJSON()</code></a></li>
218<li><a href="#buffer_buf_tostring_encoding_start_end"><code>buf.toString([encoding[, start[, end]]])</code></a></li>
219<li><a href="#buffer_buf_values"><code>buf.values()</code></a></li>
220<li><a href="#buffer_buf_write_string_offset_length_encoding"><code>buf.write(string[, offset[, length]][, encoding])</code></a></li>
221<li><a href="#buffer_buf_writebigint64be_value_offset"><code>buf.writeBigInt64BE(value[, offset])</code></a></li>
222<li><a href="#buffer_buf_writebigint64le_value_offset"><code>buf.writeBigInt64LE(value[, offset])</code></a></li>
223<li><a href="#buffer_buf_writebiguint64be_value_offset"><code>buf.writeBigUInt64BE(value[, offset])</code></a></li>
224<li><a href="#buffer_buf_writebiguint64le_value_offset"><code>buf.writeBigUInt64LE(value[, offset])</code></a></li>
225<li><a href="#buffer_buf_writedoublebe_value_offset"><code>buf.writeDoubleBE(value[, offset])</code></a></li>
226<li><a href="#buffer_buf_writedoublele_value_offset"><code>buf.writeDoubleLE(value[, offset])</code></a></li>
227<li><a href="#buffer_buf_writefloatbe_value_offset"><code>buf.writeFloatBE(value[, offset])</code></a></li>
228<li><a href="#buffer_buf_writefloatle_value_offset"><code>buf.writeFloatLE(value[, offset])</code></a></li>
229<li><a href="#buffer_buf_writeint8_value_offset"><code>buf.writeInt8(value[, offset])</code></a></li>
230<li><a href="#buffer_buf_writeint16be_value_offset"><code>buf.writeInt16BE(value[, offset])</code></a></li>
231<li><a href="#buffer_buf_writeint16le_value_offset"><code>buf.writeInt16LE(value[, offset])</code></a></li>
232<li><a href="#buffer_buf_writeint32be_value_offset"><code>buf.writeInt32BE(value[, offset])</code></a></li>
233<li><a href="#buffer_buf_writeint32le_value_offset"><code>buf.writeInt32LE(value[, offset])</code></a></li>
234<li><a href="#buffer_buf_writeintbe_value_offset_bytelength"><code>buf.writeIntBE(value, offset, byteLength)</code></a></li>
235<li><a href="#buffer_buf_writeintle_value_offset_bytelength"><code>buf.writeIntLE(value, offset, byteLength)</code></a></li>
236<li><a href="#buffer_buf_writeuint8_value_offset"><code>buf.writeUInt8(value[, offset])</code></a></li>
237<li><a href="#buffer_buf_writeuint16be_value_offset"><code>buf.writeUInt16BE(value[, offset])</code></a></li>
238<li><a href="#buffer_buf_writeuint16le_value_offset"><code>buf.writeUInt16LE(value[, offset])</code></a></li>
239<li><a href="#buffer_buf_writeuint32be_value_offset"><code>buf.writeUInt32BE(value[, offset])</code></a></li>
240<li><a href="#buffer_buf_writeuint32le_value_offset"><code>buf.writeUInt32LE(value[, offset])</code></a></li>
241<li><a href="#buffer_buf_writeuintbe_value_offset_bytelength"><code>buf.writeUIntBE(value, offset, byteLength)</code></a></li>
242<li><a href="#buffer_buf_writeuintle_value_offset_bytelength"><code>buf.writeUIntLE(value, offset, byteLength)</code></a></li>
243<li><span class="stability_0"><a href="#buffer_new_buffer_array"><code>new Buffer(array)</code></a></span></li>
244<li><span class="stability_0"><a href="#buffer_new_buffer_arraybuffer_byteoffset_length"><code>new Buffer(arrayBuffer[, byteOffset[, length]])</code></a></span></li>
245<li><span class="stability_0"><a href="#buffer_new_buffer_buffer"><code>new Buffer(buffer)</code></a></span></li>
246<li><span class="stability_0"><a href="#buffer_new_buffer_size"><code>new Buffer(size)</code></a></span></li>
247<li><span class="stability_0"><a href="#buffer_new_buffer_string_encoding"><code>new Buffer(string[, encoding])</code></a></span></li>
248</ul>
249</li>
250<li><a href="#buffer_buffer_module_apis"><code>buffer</code> module APIs</a>
251<ul>
252<li><a href="#buffer_buffer_atob_data"><code>buffer.atob(data)</code></a></li>
253<li><a href="#buffer_buffer_btoa_data"><code>buffer.btoa(data)</code></a></li>
254<li><a href="#buffer_buffer_inspect_max_bytes"><code>buffer.INSPECT_MAX_BYTES</code></a></li>
255<li><a href="#buffer_buffer_kmaxlength"><code>buffer.kMaxLength</code></a></li>
256<li><a href="#buffer_buffer_kstringmaxlength"><code>buffer.kStringMaxLength</code></a></li>
257<li><a href="#buffer_buffer_transcode_source_fromenc_toenc"><code>buffer.transcode(source, fromEnc, toEnc)</code></a></li>
258<li><span class="stability_0"><a href="#buffer_class_slowbuffer">Class: <code>SlowBuffer</code></a></span>
259<ul>
260<li><span class="stability_0"><a href="#buffer_new_slowbuffer_size"><code>new SlowBuffer(size)</code></a></span></li>
261</ul>
262</li>
263<li><a href="#buffer_buffer_constants">Buffer constants</a>
264<ul>
265<li><a href="#buffer_buffer_constants_max_length"><code>buffer.constants.MAX_LENGTH</code></a></li>
266<li><a href="#buffer_buffer_constants_max_string_length"><code>buffer.constants.MAX_STRING_LENGTH</code></a></li>
267</ul>
268</li>
269</ul>
270</li>
271<li><a href="#buffer_buffer_from_buffer_alloc_and_buffer_allocunsafe"><code>Buffer.from()</code>, <code>Buffer.alloc()</code>, and <code>Buffer.allocUnsafe()</code></a>
272<ul>
273<li><a href="#buffer_the_zero_fill_buffers_command_line_option">The <code>--zero-fill-buffers</code> command-line option</a></li>
274<li><a href="#buffer_what_makes_buffer_allocunsafe_and_buffer_allocunsafeslow_unsafe">What makes <code>Buffer.allocUnsafe()</code> and <code>Buffer.allocUnsafeSlow()</code> "unsafe"?</a></li>
275</ul>
276</li>
277</ul>
278</li>
279</ul></details>
280
281      <div id="apicontent">
282        <h2>Buffer<span><a class="mark" href="#buffer_buffer" id="buffer_buffer">#</a></span></h2>
283
284<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
285<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v14.19.1/lib/buffer.js">lib/buffer.js</a></p>
286<p><code>Buffer</code> objects are used to represent a fixed-length sequence of bytes. Many
287Node.js APIs support <code>Buffer</code>s.</p>
288<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
289extends it with methods that cover additional use cases. Node.js APIs accept
290plain <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>
291<p>The <code>Buffer</code> class is within the global scope, making it unlikely that one
292would need to ever use <code>require('buffer').Buffer</code>.</p>
293<pre><code class="language-js"><span class="hljs-comment">// Creates a zero-filled Buffer of length 10.</span>
294<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>);
295
296<span class="hljs-comment">// Creates a Buffer of length 10,</span>
297<span class="hljs-comment">// filled with bytes which all have the value `1`.</span>
298<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>);
299
300<span class="hljs-comment">// Creates an uninitialized buffer of length 10.</span>
301<span class="hljs-comment">// This is faster than calling Buffer.alloc() but the returned</span>
302<span class="hljs-comment">// Buffer instance might contain old data that needs to be</span>
303<span class="hljs-comment">// overwritten using fill(), write(), or other functions that fill the Buffer's</span>
304<span class="hljs-comment">// contents.</span>
305<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>);
306
307<span class="hljs-comment">// Creates a Buffer containing the bytes [1, 2, 3].</span>
308<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>]);
309
310<span class="hljs-comment">// Creates a Buffer containing the bytes [1, 1, 1, 1] – the entries</span>
311<span class="hljs-comment">// are all truncated using `(value &#x26; 255)` to fit into the range 0–255.</span>
312<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>]);
313
314<span class="hljs-comment">// Creates a Buffer containing the UTF-8-encoded bytes for the string 'tést':</span>
315<span class="hljs-comment">// [0x74, 0xc3, 0xa9, 0x73, 0x74] (in hexadecimal notation)</span>
316<span class="hljs-comment">// [116, 195, 169, 115, 116] (in decimal notation)</span>
317<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>);
318
319<span class="hljs-comment">// Creates a Buffer containing the Latin-1 bytes [0x74, 0xe9, 0x73, 0x74].</span>
320<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></pre>
321<section><h3>Buffers and character encodings<span><a class="mark" href="#buffer_buffers_and_character_encodings" id="buffer_buffers_and_character_encodings">#</a></span></h3>
322<div class="api_metadata">
323<details class="changelog"><summary>History</summary>
324<table>
325<tbody><tr><th>Version</th><th>Changes</th></tr>
326<tr><td>v14.18.0</td>
327<td><p>Introduced <code>base64url</code> encoding.</p></td></tr>
328<tr><td>v6.4.0</td>
329<td><p>Introduced <code>latin1</code> as an alias for <code>binary</code>.</p></td></tr>
330<tr><td>v5.0.0</td>
331<td><p>Removed the deprecated <code>raw</code> and <code>raws</code> encodings.</p></td></tr>
332</tbody></table>
333</details>
334</div>
335<p>When converting between <code>Buffer</code>s and strings, a character encoding may be
336specified. If no character encoding is specified, UTF-8 will be used as the
337default.</p>
338<pre><code class="language-js"><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>);
339
340<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>));
341<span class="hljs-comment">// Prints: 68656c6c6f20776f726c64</span>
342<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>));
343<span class="hljs-comment">// Prints: aGVsbG8gd29ybGQ=</span>
344
345<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>));
346<span class="hljs-comment">// Prints: &#x3C;Buffer 66 68 71 77 68 67 61 64 73></span>
347<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>));
348<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></pre>
349<p>The character encodings currently supported by Node.js are the following:</p>
350<ul>
351<li>
352<p><code>'utf8'</code>: Multi-byte encoded Unicode characters. Many web pages and other
353document formats use <a href="https://en.wikipedia.org/wiki/UTF-8">UTF-8</a>. This is the default character encoding.
354When decoding a <code>Buffer</code> into a string that does not exclusively contain
355valid UTF-8 data, the Unicode replacement character <code>U+FFFD</code> � will be used
356to represent those errors.</p>
357</li>
358<li>
359<p><code>'utf16le'</code>: Multi-byte encoded Unicode characters. Unlike <code>'utf8'</code>, each
360character in the string will be encoded using either 2 or 4 bytes.
361Node.js only supports the <a href="https://en.wikipedia.org/wiki/Endianness">little-endian</a> variant of <a href="https://en.wikipedia.org/wiki/UTF-16">UTF-16</a>.</p>
362</li>
363<li>
364<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
365supports the Unicode characters from <code>U+0000</code> to <code>U+00FF</code>. Each character is
366encoded using a single byte. Characters that do not fit into that range are
367truncated and will be mapped to characters in that range.</p>
368</li>
369</ul>
370<p>Converting a <code>Buffer</code> into a string using one of the above is referred to as
371decoding, and converting a string into a <code>Buffer</code> is referred to as encoding.</p>
372<p>Node.js also supports the following binary-to-text encodings. For
373binary-to-text encodings, the naming convention is reversed: Converting a
374<code>Buffer</code> into a string is typically referred to as encoding, and converting a
375string into a <code>Buffer</code> as decoding.</p>
376<ul>
377<li>
378<p><code>'base64'</code>: <a href="https://en.wikipedia.org/wiki/Base64">Base64</a> encoding. When creating a <code>Buffer</code> from a string,
379this encoding will also correctly accept "URL and Filename Safe Alphabet" as
380specified in <a href="https://tools.ietf.org/html/rfc4648#section-5">RFC 4648, Section 5</a>. Whitespace characters such as spaces,
381tabs, and new lines contained within the base64-encoded string are ignored.</p>
382</li>
383<li>
384<p><code>'base64url'</code>: <a href="https://tools.ietf.org/html/rfc4648#section-5">base64url</a> encoding as specified in
385<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
386encoding will also correctly accept regular base64-encoded strings. When
387encoding a <code>Buffer</code> to a string, this encoding will omit padding.</p>
388</li>
389<li>
390<p><code>'hex'</code>: Encode each byte as two hexadecimal characters. Data truncation
391may occur when decoding strings that do exclusively contain valid hexadecimal
392characters. See below for an example.</p>
393</li>
394</ul>
395<p>The following legacy character encodings are also supported:</p>
396<ul>
397<li>
398<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
399<code>Buffer</code>, this is equivalent to using <code>'latin1'</code>. When decoding a <code>Buffer</code>
400into a string, using this encoding will additionally unset the highest bit of
401each byte before decoding as <code>'latin1'</code>.
402Generally, there should be no reason to use this encoding, as <code>'utf8'</code>
403(or, if the data is known to always be ASCII-only, <code>'latin1'</code>) will be a
404better choice when encoding or decoding ASCII-only text. It is only provided
405for legacy compatibility.</p>
406</li>
407<li>
408<p><code>'binary'</code>: Alias for <code>'latin1'</code>. See <a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMString/Binary">binary strings</a> for more background
409on this topic. The name of this encoding can be very misleading, as all of the
410encodings listed here convert between strings and binary data. For converting
411between strings and <code>Buffer</code>s, typically <code>'utf-8'</code> is the right choice.</p>
412</li>
413<li>
414<p><code>'ucs2'</code>: Alias of <code>'utf16le'</code>. UCS-2 used to refer to a variant of UTF-16
415that did not support characters that had code points larger than U+FFFF.
416In Node.js, these code points are always supported.</p>
417</li>
418</ul>
419<pre><code class="language-js"><span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'1ag'</span>, <span class="hljs-string">'hex'</span>);
420<span class="hljs-comment">// Prints &#x3C;Buffer 1a>, data truncated when first non-hexadecimal value</span>
421<span class="hljs-comment">// ('g') encountered.</span>
422
423<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(<span class="hljs-string">'1a7g'</span>, <span class="hljs-string">'hex'</span>);
424<span class="hljs-comment">// Prints &#x3C;Buffer 1a>, data truncated when data ends in single digit ('7').</span>
425
426<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>);
427<span class="hljs-comment">// Prints &#x3C;Buffer 16 34>, all data represented.</span></code></pre>
428<p>Modern Web browsers follow the <a href="https://encoding.spec.whatwg.org/">WHATWG Encoding Standard</a> which aliases
429both <code>'latin1'</code> and <code>'ISO-8859-1'</code> to <code>'win-1252'</code>. This means that while doing
430something like <code>http.get()</code>, if the returned charset is one of those listed in
431the WHATWG specification it is possible that the server actually returned
432<code>'win-1252'</code>-encoded data, and using <code>'latin1'</code> encoding may incorrectly decode
433the characters.</p>
434</section><section><h3>Buffers and TypedArrays<span><a class="mark" href="#buffer_buffers_and_typedarrays" id="buffer_buffers_and_typedarrays">#</a></span></h3>
435<div class="api_metadata">
436<details class="changelog"><summary>History</summary>
437<table>
438<tbody><tr><th>Version</th><th>Changes</th></tr>
439<tr><td>v3.0.0</td>
440<td><p>The <code>Buffer</code>s class now inherits from <code>Uint8Array</code>.</p></td></tr>
441</tbody></table>
442</details>
443</div>
444<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>
445instances. 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,
446however, subtle incompatibilities between the <code>Buffer</code> API and the
447<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a> API.</p>
448<p>In particular:</p>
449<ul>
450<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>,
451<a href="#buffer_buf_slice_start_end"><code>Buffer.prototype.slice()</code></a> creates a view over the existing <code>Buffer</code>
452without copying. This behavior can be surprising, and only exists for legacy
453compatibility. <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
454the behavior of <a href="#buffer_buf_slice_start_end"><code>Buffer.prototype.slice()</code></a> on both <code>Buffer</code>s
455and other <code>TypedArray</code>s.</li>
456<li><a href="#buffer_buf_tostring_encoding_start_end"><code>buf.toString()</code></a> is incompatible with its <code>TypedArray</code> equivalent.</li>
457<li>A number of methods, e.g. <a href="#buffer_buf_indexof_value_byteoffset_encoding"><code>buf.indexOf()</code></a>, support additional arguments.</li>
458</ul>
459<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>
460<ul>
461<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
462contents, interpreted as an array of integers, and not as a byte sequence
463of the target type.</li>
464</ul>
465<pre><code class="language-js"><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>]);
466<span class="hljs-keyword">const</span> uint32array = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint</span>32<span class="hljs-built_in">Array</span>(buf);
467
468<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(uint32array);
469
470<span class="hljs-comment">// Prints: Uint32Array(4) [ 1, 2, 3, 4 ]</span></code></pre>
471<ul>
472<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
473<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>
474</ul>
475<pre><code class="language-js"><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>);
476<span class="hljs-keyword">const</span> uint16array = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint</span>16<span class="hljs-built_in">Array</span>(
477  buf.<span class="hljs-property">buffer</span>,
478  buf.<span class="hljs-property">byteOffset</span>,
479  buf.<span class="hljs-property">length</span> / <span class="hljs-title class_">Uint</span>16<span class="hljs-built_in">Array</span>.<span class="hljs-property">BYTES_PER_ELEMENT</span>);
480
481<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(uint16array);
482
483<span class="hljs-comment">// Prints: Uint16Array(5) [ 104, 101, 108, 108, 111 ]</span></code></pre>
484<p>It is possible to create a new <code>Buffer</code> that shares the same allocated
485memory 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
486<code>.buffer</code> property in the same way. <a href="#buffer_static_method_buffer_from_arraybuffer_byteoffset_length"><code>Buffer.from()</code></a>
487behaves like <code>new Uint8Array()</code> in this context.</p>
488<pre><code class="language-js"><span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint</span>16<span class="hljs-built_in">Array</span>(<span class="hljs-number">2</span>);
489
490arr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>;
491arr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>;
492
493<span class="hljs-comment">// Copies the contents of `arr`.</span>
494<span class="hljs-keyword">const</span> buf1 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arr);
495
496<span class="hljs-comment">// Shares memory with `arr`.</span>
497<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>);
498
499<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
500<span class="hljs-comment">// Prints: &#x3C;Buffer 88 a0></span>
501<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2);
502<span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 a0 0f></span>
503
504arr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>;
505
506<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
507<span class="hljs-comment">// Prints: &#x3C;Buffer 88 a0></span>
508<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2);
509<span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 70 17></span></code></pre>
510<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
511possible 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
512<code>byteOffset</code> and <code>length</code> parameters.</p>
513<pre><code class="language-js"><span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint</span>16<span class="hljs-built_in">Array</span>(<span class="hljs-number">20</span>);
514<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>);
515
516<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>);
517<span class="hljs-comment">// Prints: 16</span></code></pre>
518<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
519implementations. 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
520argument that is a mapping function that is invoked on every element of the
521typed array:</p>
522<ul>
523<li><code>TypedArray.from(source[, mapFn[, thisArg]])</code></li>
524</ul>
525<p>The <code>Buffer.from()</code> method, however, does not support the use of a mapping
526function:</p>
527<ul>
528<li><a href="#buffer_static_method_buffer_from_array"><code>Buffer.from(array)</code></a></li>
529<li><a href="#buffer_static_method_buffer_from_buffer"><code>Buffer.from(buffer)</code></a></li>
530<li><a href="#buffer_static_method_buffer_from_arraybuffer_byteoffset_length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a></li>
531<li><a href="#buffer_static_method_buffer_from_string_encoding"><code>Buffer.from(string[, encoding])</code></a></li>
532</ul>
533</section><section><h3>Buffers and iteration<span><a class="mark" href="#buffer_buffers_and_iteration" id="buffer_buffers_and_iteration">#</a></span></h3>
534<p><code>Buffer</code> instances can be iterated over using <code>for..of</code> syntax:</p>
535<pre><code class="language-js"><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>]);
536
537<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> b <span class="hljs-keyword">of</span> buf) {
538  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b);
539}
540<span class="hljs-comment">// Prints:</span>
541<span class="hljs-comment">//   1</span>
542<span class="hljs-comment">//   2</span>
543<span class="hljs-comment">//   3</span></code></pre>
544<p>Additionally, the <a href="#buffer_buf_values"><code>buf.values()</code></a>, <a href="#buffer_buf_keys"><code>buf.keys()</code></a>, and
545<a href="#buffer_buf_entries"><code>buf.entries()</code></a> methods can be used to create iterators.</p>
546</section><section><h3>Class: <code>Blob</code><span><a class="mark" href="#buffer_class_blob" id="buffer_class_blob">#</a></span></h3>
547<div class="api_metadata">
548<span>Added in: v14.18.0</span>
549</div>
550<p></p><div class="api_stability api_stability_1"><a href="documentation.html#documentation_stability_index">Stability: 1</a> - Experimental</div><p></p>
551<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
552multiple worker threads.</p>
553<h4><code>new buffer.Blob([sources[, options]])</code><span><a class="mark" href="#buffer_new_buffer_blob_sources_options" id="buffer_new_buffer_blob_sources_options">#</a></span></h4>
554<div class="api_metadata">
555<span>Added in: v14.18.0</span>
556</div>
557<ul>
558<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#buffer_class_blob" class="type">&#x3C;Blob[]></a> An array
559of 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#buffer_class_blob" class="type">&#x3C;Blob></a> objects, or
560any mix of such objects, that will be stored within the <code>Blob</code>.</li>
561<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
562<ul>
563<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 for string sources.
564<strong>Default</strong>: <code>'utf8'</code>.</li>
565<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
566the MIME media type of the data, however no validation of the type format
567is performed.</li>
568</ul>
569</li>
570</ul>
571<p>Creates a new <code>Blob</code> object containing a concatenation of the given sources.</p>
572<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#buffer_class_buffer" class="type">&#x3C;Buffer></a> sources are copied into
573the 'Blob' and can therefore be safely modified after the 'Blob' is created.</p>
574<p>String sources are also copied into the <code>Blob</code>.</p>
575<h4><code>blob.arrayBuffer()</code><span><a class="mark" href="#buffer_blob_arraybuffer" id="buffer_blob_arraybuffer">#</a></span></h4>
576<div class="api_metadata">
577<span>Added in: v14.18.0</span>
578</div>
579<ul>
580<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a></li>
581</ul>
582<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
583the <code>Blob</code> data.</p>
584<h4><code>blob.size</code><span><a class="mark" href="#buffer_blob_size" id="buffer_blob_size">#</a></span></h4>
585<div class="api_metadata">
586<span>Added in: v14.18.0</span>
587</div>
588<p>The total size of the <code>Blob</code> in bytes.</p>
589<h4><code>blob.slice([start, [end, [type]]])</code><span><a class="mark" href="#buffer_blob_slice_start_end_type" id="buffer_blob_slice_start_end_type">#</a></span></h4>
590<div class="api_metadata">
591<span>Added in: v14.18.0</span>
592</div>
593<ul>
594<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>
595<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>
596<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>
597</ul>
598<p>Creates and returns a new <code>Blob</code> containing a subset of this <code>Blob</code> objects
599data. The original <code>Blob</code> is not alterered.</p>
600<h4><code>blob.text()</code><span><a class="mark" href="#buffer_blob_text" id="buffer_blob_text">#</a></span></h4>
601<div class="api_metadata">
602<span>Added in: v14.18.0</span>
603</div>
604<ul>
605<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type">&#x3C;Promise></a></li>
606</ul>
607<p>Returns a promise that resolves the contents of the <code>Blob</code> decoded as a UTF-8
608string.</p>
609<h4><code>blob.type</code><span><a class="mark" href="#buffer_blob_type" id="buffer_blob_type">#</a></span></h4>
610<div class="api_metadata">
611<span>Added in: v14.18.0</span>
612</div>
613<ul>
614<li>Type: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
615</ul>
616<p>The content-type of the <code>Blob</code>.</p>
617<h4><code>Blob</code> objects and <code>MessageChannel</code><span><a class="mark" href="#buffer_blob_objects_and_messagechannel" id="buffer_blob_objects_and_messagechannel">#</a></span></h4>
618<p>Once a <a href="buffer.html#buffer_class_blob" class="type">&#x3C;Blob></a> object is created, it can be sent via <code>MessagePort</code> to multiple
619destinations without transfering or immediately copying the data. The data
620contained by the <code>Blob</code> is copied only when the <code>arrayBuffer()</code> or <code>text()</code>
621methods are called.</p>
622<pre><code class="language-js"><span class="hljs-keyword">const</span> { <span class="hljs-title class_">Blob</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'buffer'</span>);
623<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>]);
624<span class="hljs-keyword">const</span> { <span class="hljs-attr">setTimeout</span>: delay } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'timers/promises'</span>);
625
626<span class="hljs-keyword">const</span> mc1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>();
627<span class="hljs-keyword">const</span> mc2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MessageChannel</span>();
628
629mc1.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">async</span> ({ data }) => {
630  <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>());
631  mc1.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>();
632};
633
634mc2.<span class="hljs-property">port1</span>.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">async</span> ({ data }) => {
635  <span class="hljs-keyword">await</span> <span class="hljs-title function_">delay</span>(<span class="hljs-number">1000</span>);
636  <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>());
637  mc2.<span class="hljs-property">port1</span>.<span class="hljs-title function_">close</span>();
638};
639
640mc1.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(blob);
641mc2.<span class="hljs-property">port2</span>.<span class="hljs-title function_">postMessage</span>(blob);
642
643<span class="hljs-comment">// The Blob is still usable after posting.</span>
644data.<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></pre>
645</section><section><h3>Class: <code>Buffer</code><span><a class="mark" href="#buffer_class_buffer" id="buffer_class_buffer">#</a></span></h3>
646<p>The <code>Buffer</code> class is a global type for dealing with binary data directly.
647It can be constructed in a variety of ways.</p>
648<h4>Static method: <code>Buffer.alloc(size[, fill[, encoding]])</code><span><a class="mark" href="#buffer_static_method_buffer_alloc_size_fill_encoding" id="buffer_static_method_buffer_alloc_size_fill_encoding">#</a></span></h4>
649<div class="api_metadata">
650<details class="changelog"><summary>History</summary>
651<table>
652<tbody><tr><th>Version</th><th>Changes</th></tr>
653<tr><td>v10.0.0</td>
654<td><p>Attempting to fill a non-zero length buffer with a zero length buffer triggers a thrown exception.</p></td></tr>
655<tr><td>v10.0.0</td>
656<td><p>Specifying an invalid string for <code>fill</code> triggers a thrown exception.</p></td></tr>
657<tr><td>v8.9.3</td>
658<td><p>Specifying an invalid string for <code>fill</code> now results in a zero-filled buffer.</p></td></tr>
659<tr><td>v5.10.0</td>
660<td><p><span>Added in: v5.10.0</span></p></td></tr>
661</tbody></table>
662</details>
663</div>
664<ul>
665<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>
666<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#buffer_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>
667with. <strong>Default:</strong> <code>0</code>.</li>
668<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.
669<strong>Default:</strong> <code>'utf8'</code>.</li>
670</ul>
671<p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>fill</code> is <code>undefined</code>, the
672<code>Buffer</code> will be zero-filled.</p>
673<pre><code class="language-js"><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>);
674
675<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
676<span class="hljs-comment">// Prints: &#x3C;Buffer 00 00 00 00 00></span></code></pre>
677<p>If <code>size</code> is larger than
678<a href="#buffer_buffer_constants_max_length"><code>buffer.constants.MAX_LENGTH</code></a> or smaller than 0, <a href="errors.html#ERR_INVALID_OPT_VALUE"><code>ERR_INVALID_OPT_VALUE</code></a>
679is thrown.</p>
680<p>If <code>fill</code> is specified, the allocated <code>Buffer</code> will be initialized by calling
681<a href="#buffer_buf_fill_value_offset_end_encoding"><code>buf.fill(fill)</code></a>.</p>
682<pre><code class="language-js"><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>);
683
684<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
685<span class="hljs-comment">// Prints: &#x3C;Buffer 61 61 61 61 61></span></code></pre>
686<p>If both <code>fill</code> and <code>encoding</code> are specified, the allocated <code>Buffer</code> will be
687initialized by calling <a href="#buffer_buf_fill_value_offset_end_encoding"><code>buf.fill(fill, encoding)</code></a>.</p>
688<pre><code class="language-js"><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>);
689
690<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
691<span class="hljs-comment">// Prints: &#x3C;Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64></span></code></pre>
692<p>Calling <a href="#buffer_static_method_buffer_alloc_size_fill_encoding"><code>Buffer.alloc()</code></a> can be measurably slower than the alternative
693<a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a> but ensures that the newly created <code>Buffer</code> instance
694contents will never contain sensitive data from previous allocations, including
695data that might not have been allocated for <code>Buffer</code>s.</p>
696<p>A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</p>
697<h4>Static method: <code>Buffer.allocUnsafe(size)</code><span><a class="mark" href="#buffer_static_method_buffer_allocunsafe_size" id="buffer_static_method_buffer_allocunsafe_size">#</a></span></h4>
698<div class="api_metadata">
699<details class="changelog"><summary>History</summary>
700<table>
701<tbody><tr><th>Version</th><th>Changes</th></tr>
702<tr><td>v7.0.0</td>
703<td><p>Passing a negative <code>size</code> will now throw an error.</p></td></tr>
704<tr><td>v5.10.0</td>
705<td><p><span>Added in: v5.10.0</span></p></td></tr>
706</tbody></table>
707</details>
708</div>
709<ul>
710<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>
711</ul>
712<p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>size</code> is larger than
713<a href="#buffer_buffer_constants_max_length"><code>buffer.constants.MAX_LENGTH</code></a> or smaller than 0, <a href="errors.html#ERR_INVALID_OPT_VALUE"><code>ERR_INVALID_OPT_VALUE</code></a>
714is thrown.</p>
715<p>The underlying memory for <code>Buffer</code> instances created in this way is <em>not
716initialized</em>. The contents of the newly created <code>Buffer</code> are unknown and
717<em>may contain sensitive data</em>. Use <a href="#buffer_static_method_buffer_alloc_size_fill_encoding"><code>Buffer.alloc()</code></a> instead to initialize
718<code>Buffer</code> instances with zeroes.</p>
719<pre><code class="language-js"><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>);
720
721<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
722<span class="hljs-comment">// Prints (contents may vary): &#x3C;Buffer a0 8b 28 3f 01 00 00 00 50 32></span>
723
724buf.<span class="hljs-title function_">fill</span>(<span class="hljs-number">0</span>);
725
726<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
727<span class="hljs-comment">// Prints: &#x3C;Buffer 00 00 00 00 00 00 00 00 00 00></span></code></pre>
728<p>A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</p>
729<p>The <code>Buffer</code> module pre-allocates an internal <code>Buffer</code> instance of
730size <a href="#buffer_class_property_buffer_poolsize"><code>Buffer.poolSize</code></a> that is used as a pool for the fast allocation of new
731<code>Buffer</code> instances created using <a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a>,
732<a href="#buffer_static_method_buffer_from_array"><code>Buffer.from(array)</code></a>, <a href="#buffer_static_method_buffer_concat_list_totallength"><code>Buffer.concat()</code></a>, and the deprecated
733<code>new Buffer(size)</code> constructor only when <code>size</code> is less than or equal
734to <code>Buffer.poolSize >> 1</code> (floor of <a href="#buffer_class_property_buffer_poolsize"><code>Buffer.poolSize</code></a> divided by two).</p>
735<p>Use of this pre-allocated internal memory pool is a key difference between
736calling <code>Buffer.alloc(size, fill)</code> vs. <code>Buffer.allocUnsafe(size).fill(fill)</code>.
737Specifically, <code>Buffer.alloc(size, fill)</code> will <em>never</em> use the internal <code>Buffer</code>
738pool, while <code>Buffer.allocUnsafe(size).fill(fill)</code> <em>will</em> use the internal
739<code>Buffer</code> pool if <code>size</code> is less than or equal to half <a href="#buffer_class_property_buffer_poolsize"><code>Buffer.poolSize</code></a>. The
740difference is subtle but can be important when an application requires the
741additional performance that <a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a> provides.</p>
742<h4>Static method: <code>Buffer.allocUnsafeSlow(size)</code><span><a class="mark" href="#buffer_static_method_buffer_allocunsafeslow_size" id="buffer_static_method_buffer_allocunsafeslow_size">#</a></span></h4>
743<div class="api_metadata">
744<span>Added in: v5.12.0</span>
745</div>
746<ul>
747<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>
748</ul>
749<p>Allocates a new <code>Buffer</code> of <code>size</code> bytes. If <code>size</code> is larger than
750<a href="#buffer_buffer_constants_max_length"><code>buffer.constants.MAX_LENGTH</code></a> or smaller than 0, <a href="errors.html#ERR_INVALID_OPT_VALUE"><code>ERR_INVALID_OPT_VALUE</code></a>
751is thrown. A zero-length <code>Buffer</code> is created if <code>size</code> is 0.</p>
752<p>The underlying memory for <code>Buffer</code> instances created in this way is <em>not
753initialized</em>. The contents of the newly created <code>Buffer</code> are unknown and
754<em>may contain sensitive data</em>. Use <a href="#buffer_buf_fill_value_offset_end_encoding"><code>buf.fill(0)</code></a> to initialize
755such <code>Buffer</code> instances with zeroes.</p>
756<p>When using <a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a> to allocate new <code>Buffer</code> instances,
757allocations under 4KB are sliced from a single pre-allocated <code>Buffer</code>. This
758allows applications to avoid the garbage collection overhead of creating many
759individually allocated <code>Buffer</code> instances. This approach improves both
760performance and memory usage by eliminating the need to track and clean up as
761many individual <code>ArrayBuffer</code> objects.</p>
762<p>However, in the case where a developer may need to retain a small chunk of
763memory from a pool for an indeterminate amount of time, it may be appropriate
764to create an un-pooled <code>Buffer</code> instance using <code>Buffer.allocUnsafeSlow()</code> and
765then copying out the relevant bits.</p>
766<pre><code class="language-js"><span class="hljs-comment">// Need to keep around a few small chunks of memory.</span>
767<span class="hljs-keyword">const</span> store = [];
768
769socket.<span class="hljs-title function_">on</span>(<span class="hljs-string">'readable'</span>, <span class="hljs-function">() =></span> {
770  <span class="hljs-keyword">let</span> data;
771  <span class="hljs-keyword">while</span> (<span class="hljs-literal">null</span> !== (data = readable.<span class="hljs-title function_">read</span>())) {
772    <span class="hljs-comment">// Allocate for retained data.</span>
773    <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>);
774
775    <span class="hljs-comment">// Copy the data into the new allocation.</span>
776    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>);
777
778    store.<span class="hljs-title function_">push</span>(sb);
779  }
780});</code></pre>
781<p>A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</p>
782<h4>Static method: <code>Buffer.byteLength(string[, encoding])</code><span><a class="mark" href="#buffer_static_method_buffer_bytelength_string_encoding" id="buffer_static_method_buffer_bytelength_string_encoding">#</a></span></h4>
783<div class="api_metadata">
784<details class="changelog"><summary>History</summary>
785<table>
786<tbody><tr><th>Version</th><th>Changes</th></tr>
787<tr><td>v7.0.0</td>
788<td><p>Passing invalid input will now throw an error.</p></td></tr>
789<tr><td>v5.10.0</td>
790<td><p>The <code>string</code> parameter can now be any <code>TypedArray</code>, <code>DataView</code> or <code>ArrayBuffer</code>.</p></td></tr>
791<tr><td>v0.1.90</td>
792<td><p><span>Added in: v0.1.90</span></p></td></tr>
793</tbody></table>
794</details>
795</div>
796<ul>
797<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#buffer_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
798value to calculate the length of.</li>
799<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.
800<strong>Default:</strong> <code>'utf8'</code>.</li>
801<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>
802</ul>
803<p>Returns the byte length of a string when encoded using <code>encoding</code>.
804This 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
805for the encoding that is used to convert the string into bytes.</p>
806<p>For <code>'base64'</code>, <code>'base64url'</code>, and <code>'hex'</code>, this function assumes valid input.
807For strings that contain non-base64/hex-encoded data (e.g. whitespace), the
808return value might be greater than the length of a <code>Buffer</code> created from the
809string.</p>
810<pre><code class="language-js"><span class="hljs-keyword">const</span> str = <span class="hljs-string">'\u00bd + \u00bc = \u00be'</span>;
811
812<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> +
813            <span class="hljs-string">`<span class="hljs-subst">${Buffer.byteLength(str, <span class="hljs-string">'utf8'</span>)}</span> bytes`</span>);
814<span class="hljs-comment">// Prints: ½ + ¼ = ¾: 9 characters, 12 bytes</span></code></pre>
815<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>/
816<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>
817is returned.</p>
818<h4>Static method: <code>Buffer.compare(buf1, buf2)</code><span><a class="mark" href="#buffer_static_method_buffer_compare_buf1_buf2" id="buffer_static_method_buffer_compare_buf1_buf2">#</a></span></h4>
819<div class="api_metadata">
820<details class="changelog"><summary>History</summary>
821<table>
822<tbody><tr><th>Version</th><th>Changes</th></tr>
823<tr><td>v8.0.0</td>
824<td><p>The arguments can now be <code>Uint8Array</code>s.</p></td></tr>
825<tr><td>v0.11.13</td>
826<td><p><span>Added in: v0.11.13</span></p></td></tr>
827</tbody></table>
828</details>
829</div>
830<ul>
831<li><code>buf1</code> <a href="buffer.html#buffer_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>
832<li><code>buf2</code> <a href="buffer.html#buffer_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>
833<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
834comparison. See <a href="#buffer_buf_compare_target_targetstart_targetend_sourcestart_sourceend"><code>buf.compare()</code></a> for details.</li>
835</ul>
836<p>Compares <code>buf1</code> to <code>buf2</code>, typically for the purpose of sorting arrays of
837<code>Buffer</code> instances. This is equivalent to calling
838<a href="#buffer_buf_compare_target_targetstart_targetend_sourcestart_sourceend"><code>buf1.compare(buf2)</code></a>.</p>
839<pre><code class="language-js"><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>);
840<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>);
841<span class="hljs-keyword">const</span> arr = [buf1, buf2];
842
843<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>));
844<span class="hljs-comment">// Prints: [ &#x3C;Buffer 30 31 32 33>, &#x3C;Buffer 31 32 33 34> ]</span>
845<span class="hljs-comment">// (This result is equal to: [buf2, buf1].)</span></code></pre>
846<h4>Static method: <code>Buffer.concat(list[, totalLength])</code><span><a class="mark" href="#buffer_static_method_buffer_concat_list_totallength" id="buffer_static_method_buffer_concat_list_totallength">#</a></span></h4>
847<div class="api_metadata">
848<details class="changelog"><summary>History</summary>
849<table>
850<tbody><tr><th>Version</th><th>Changes</th></tr>
851<tr><td>v8.0.0</td>
852<td><p>The elements of <code>list</code> can now be <code>Uint8Array</code>s.</p></td></tr>
853<tr><td>v0.7.11</td>
854<td><p><span>Added in: v0.7.11</span></p></td></tr>
855</tbody></table>
856</details>
857</div>
858<ul>
859<li><code>list</code> <a href="buffer.html#buffer_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>
860instances to concatenate.</li>
861<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>
862when concatenated.</li>
863<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
864</ul>
865<p>Returns a new <code>Buffer</code> which is the result of concatenating all the <code>Buffer</code>
866instances in the <code>list</code> together.</p>
867<p>If the list has no items, or if the <code>totalLength</code> is 0, then a new zero-length
868<code>Buffer</code> is returned.</p>
869<p>If <code>totalLength</code> is not provided, it is calculated from the <code>Buffer</code> instances
870in <code>list</code> by adding their lengths.</p>
871<p>If <code>totalLength</code> is provided, it is coerced to an unsigned integer. If the
872combined length of the <code>Buffer</code>s in <code>list</code> exceeds <code>totalLength</code>, the result is
873truncated to <code>totalLength</code>.</p>
874<pre><code class="language-js"><span class="hljs-comment">// Create a single `Buffer` from a list of three `Buffer` instances.</span>
875
876<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>);
877<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>);
878<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>);
879<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>;
880
881<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(totalLength);
882<span class="hljs-comment">// Prints: 42</span>
883
884<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);
885
886<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(bufA);
887<span class="hljs-comment">// Prints: &#x3C;Buffer 00 00 00 00 ...></span>
888<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(bufA.<span class="hljs-property">length</span>);
889<span class="hljs-comment">// Prints: 42</span></code></pre>
890<p><code>Buffer.concat()</code> may also use the internal <code>Buffer</code> pool like
891<a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a> does.</p>
892<h4>Static method: <code>Buffer.from(array)</code><span><a class="mark" href="#buffer_static_method_buffer_from_array" id="buffer_static_method_buffer_from_array">#</a></span></h4>
893<div class="api_metadata">
894<span>Added in: v5.10.0</span>
895</div>
896<ul>
897<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>
898</ul>
899<p>Allocates a new <code>Buffer</code> using an <code>array</code> of bytes in the range <code>0</code> – <code>255</code>.
900Array entries outside that range will be truncated to fit into it.</p>
901<pre><code class="language-js"><span class="hljs-comment">// Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'.</span>
902<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></pre>
903<p>A <code>TypeError</code> will be thrown if <code>array</code> is not an <code>Array</code> or another type
904appropriate for <code>Buffer.from()</code> variants.</p>
905<p><code>Buffer.from(array)</code> and <a href="#buffer_static_method_buffer_from_string_encoding"><code>Buffer.from(string)</code></a> may also use the internal
906<code>Buffer</code> pool like <a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a> does.</p>
907<h4>Static method: <code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code><span><a class="mark" href="#buffer_static_method_buffer_from_arraybuffer_byteoffset_length" id="buffer_static_method_buffer_from_arraybuffer_byteoffset_length">#</a></span></h4>
908<div class="api_metadata">
909<span>Added in: v5.10.0</span>
910</div>
911<ul>
912<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>,
913<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
914<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>.</li>
915<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>
916<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.
917<strong>Default:</strong> <code>arrayBuffer.byteLength - byteOffset</code>.</li>
918</ul>
919<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
920memory. For example, when passed a reference to the <code>.buffer</code> property of a
921<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
922allocated 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>
923<pre><code class="language-js"><span class="hljs-keyword">const</span> arr = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint</span>16<span class="hljs-built_in">Array</span>(<span class="hljs-number">2</span>);
924
925arr[<span class="hljs-number">0</span>] = <span class="hljs-number">5000</span>;
926arr[<span class="hljs-number">1</span>] = <span class="hljs-number">4000</span>;
927
928<span class="hljs-comment">// Shares memory with `arr`.</span>
929<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>);
930
931<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
932<span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 a0 0f></span>
933
934<span class="hljs-comment">// Changing the original Uint16Array changes the Buffer also.</span>
935arr[<span class="hljs-number">1</span>] = <span class="hljs-number">6000</span>;
936
937<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
938<span class="hljs-comment">// Prints: &#x3C;Buffer 88 13 70 17></span></code></pre>
939<p>The optional <code>byteOffset</code> and <code>length</code> arguments specify a memory range within
940the <code>arrayBuffer</code> that will be shared by the <code>Buffer</code>.</p>
941<pre><code class="language-js"><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>);
942<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>);
943
944<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>);
945<span class="hljs-comment">// Prints: 2</span></code></pre>
946<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
947<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>
948variants.</p>
949<p>It is important to remember that a backing <code>ArrayBuffer</code> can cover a range
950of memory that extends beyond the bounds of a <code>TypedArray</code> view. A new
951<code>Buffer</code> created using the <code>buffer</code> property of a <code>TypedArray</code> may extend
952beyond the range of the <code>TypedArray</code>:</p>
953<pre><code class="language-js"><span class="hljs-keyword">const</span> arrA = <span class="hljs-title class_">Uint</span>8<span class="hljs-built_in">Array</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>
954<span class="hljs-keyword">const</span> arrB = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Uint</span>8<span class="hljs-built_in">Array</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>
955<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>
956
957<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>);
958<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
959<span class="hljs-comment">// Prints: &#x3C;Buffer 63 64 65 66></span></code></pre>
960<h4>Static method: <code>Buffer.from(buffer)</code><span><a class="mark" href="#buffer_static_method_buffer_from_buffer" id="buffer_static_method_buffer_from_buffer">#</a></span></h4>
961<div class="api_metadata">
962<span>Added in: v5.10.0</span>
963</div>
964<ul>
965<li><code>buffer</code> <a href="buffer.html#buffer_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
966which to copy data.</li>
967</ul>
968<p>Copies the passed <code>buffer</code> data onto a new <code>Buffer</code> instance.</p>
969<pre><code class="language-js"><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>);
970<span class="hljs-keyword">const</span> buf2 = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(buf1);
971
972buf1[<span class="hljs-number">0</span>] = <span class="hljs-number">0x61</span>;
973
974<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>());
975<span class="hljs-comment">// Prints: auffer</span>
976<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>());
977<span class="hljs-comment">// Prints: buffer</span></code></pre>
978<p>A <code>TypeError</code> will be thrown if <code>buffer</code> is not a <code>Buffer</code> or another type
979appropriate for <code>Buffer.from()</code> variants.</p>
980<h4>Static method: <code>Buffer.from(object[, offsetOrEncoding[, length]])</code><span><a class="mark" href="#buffer_static_method_buffer_from_object_offsetorencoding_length" id="buffer_static_method_buffer_from_object_offsetorencoding_length">#</a></span></h4>
981<div class="api_metadata">
982<span>Added in: v8.2.0</span>
983</div>
984<ul>
985<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>
986<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>
987<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>
988</ul>
989<p>For objects whose <code>valueOf()</code> function returns a value not strictly equal to
990<code>object</code>, returns <code>Buffer.from(object.valueOf(), offsetOrEncoding, length)</code>.</p>
991<pre><code class="language-js"><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>));
992<span class="hljs-comment">// Prints: &#x3C;Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74></span></code></pre>
993<p>For objects that support <code>Symbol.toPrimitive</code>, returns
994<code>Buffer.from(object[Symbol.toPrimitive]('string'), offsetOrEncoding)</code>.</p>
995<pre><code class="language-js"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Foo</span> {
996  [<span class="hljs-title class_">Symbol</span>.<span class="hljs-property">toPrimitive</span>]() {
997    <span class="hljs-keyword">return</span> <span class="hljs-string">'this is a test'</span>;
998  }
999}
1000
1001<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>);
1002<span class="hljs-comment">// Prints: &#x3C;Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74></span></code></pre>
1003<p>A <code>TypeError</code> will be thrown if <code>object</code> does not have the mentioned methods or
1004is not of another type appropriate for <code>Buffer.from()</code> variants.</p>
1005<h4>Static method: <code>Buffer.from(string[, encoding])</code><span><a class="mark" href="#buffer_static_method_buffer_from_string_encoding" id="buffer_static_method_buffer_from_string_encoding">#</a></span></h4>
1006<div class="api_metadata">
1007<span>Added in: v5.10.0</span>
1008</div>
1009<ul>
1010<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>
1011<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>
1012</ul>
1013<p>Creates a new <code>Buffer</code> containing <code>string</code>. The <code>encoding</code> parameter identifies
1014the character encoding to be used when converting <code>string</code> into bytes.</p>
1015<pre><code class="language-js"><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>);
1016<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>);
1017
1018<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">toString</span>());
1019<span class="hljs-comment">// Prints: this is a tést</span>
1020<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">toString</span>());
1021<span class="hljs-comment">// Prints: this is a tést</span>
1022<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>));
1023<span class="hljs-comment">// Prints: this is a tést</span></code></pre>
1024<p>A <code>TypeError</code> will be thrown if <code>string</code> is not a string or another type
1025appropriate for <code>Buffer.from()</code> variants.</p>
1026<h4>Static method: <code>Buffer.isBuffer(obj)</code><span><a class="mark" href="#buffer_static_method_buffer_isbuffer_obj" id="buffer_static_method_buffer_isbuffer_obj">#</a></span></h4>
1027<div class="api_metadata">
1028<span>Added in: v0.1.101</span>
1029</div>
1030<ul>
1031<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>
1032<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1033</ul>
1034<p>Returns <code>true</code> if <code>obj</code> is a <code>Buffer</code>, <code>false</code> otherwise.</p>
1035<pre><code class="language-js"><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>
1036<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>
1037<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>
1038<span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">isBuffer</span>([]); <span class="hljs-comment">// false</span>
1039<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_">Uint</span>8<span class="hljs-built_in">Array</span>(<span class="hljs-number">1024</span>)); <span class="hljs-comment">// false</span></code></pre>
1040<h4>Static method: <code>Buffer.isEncoding(encoding)</code><span><a class="mark" href="#buffer_static_method_buffer_isencoding_encoding" id="buffer_static_method_buffer_isencoding_encoding">#</a></span></h4>
1041<div class="api_metadata">
1042<span>Added in: v0.9.1</span>
1043</div>
1044<ul>
1045<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>
1046<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1047</ul>
1048<p>Returns <code>true</code> if <code>encoding</code> is the name of a supported character encoding,
1049or <code>false</code> otherwise.</p>
1050<pre><code class="language-js"><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>));
1051<span class="hljs-comment">// Prints: true</span>
1052
1053<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>));
1054<span class="hljs-comment">// Prints: true</span>
1055
1056<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>));
1057<span class="hljs-comment">// Prints: false</span>
1058
1059<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>));
1060<span class="hljs-comment">// Prints: false</span></code></pre>
1061<h4>Class property: <code>Buffer.poolSize</code><span><a class="mark" href="#buffer_class_property_buffer_poolsize" id="buffer_class_property_buffer_poolsize">#</a></span></h4>
1062<div class="api_metadata">
1063<span>Added in: v0.11.3</span>
1064</div>
1065<ul>
1066<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>
1067</ul>
1068<p>This is the size (in bytes) of pre-allocated internal <code>Buffer</code> instances used
1069for pooling. This value may be modified.</p>
1070<h4><code>buf[index]</code><span><a class="mark" href="#buffer_buf_index" id="buffer_buf_index">#</a></span></h4>
1071<ul>
1072<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>
1073</ul>
1074<p>The index operator <code>[index]</code> can be used to get and set the octet at position
1075<code>index</code> in <code>buf</code>. The values refer to individual bytes, so the legal value
1076range is between <code>0x00</code> and <code>0xFF</code> (hex) or <code>0</code> and <code>255</code> (decimal).</p>
1077<p>This operator is inherited from <code>Uint8Array</code>, so its behavior on out-of-bounds
1078access is the same as <code>Uint8Array</code>. In other words, <code>buf[index]</code> returns
1079<code>undefined</code> when <code>index</code> is negative or greater or equal to <code>buf.length</code>, and
1080<code>buf[index] = value</code> does not modify the buffer if <code>index</code> is negative or
1081<code>>= buf.length</code>.</p>
1082<pre><code class="language-js"><span class="hljs-comment">// Copy an ASCII string into a `Buffer` one byte at a time.</span>
1083<span class="hljs-comment">// (This only works for ASCII-only strings. In general, one should use</span>
1084<span class="hljs-comment">// `Buffer.from()` to perform this conversion.)</span>
1085
1086<span class="hljs-keyword">const</span> str = <span class="hljs-string">'Node.js'</span>;
1087<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>);
1088
1089<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++) {
1090  buf[i] = str.<span class="hljs-title function_">charCodeAt</span>(i);
1091}
1092
1093<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>));
1094<span class="hljs-comment">// Prints: Node.js</span></code></pre>
1095<h4><code>buf.buffer</code><span><a class="mark" href="#buffer_buf_buffer" id="buffer_buf_buffer">#</a></span></h4>
1096<ul>
1097<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>
1098object is created.</li>
1099</ul>
1100<p>This <code>ArrayBuffer</code> is not guaranteed to correspond exactly to the original
1101<code>Buffer</code>. See the notes on <code>buf.byteOffset</code> for details.</p>
1102<pre><code class="language-js"><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>);
1103<span class="hljs-keyword">const</span> buffer = <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(arrayBuffer);
1104
1105<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buffer.<span class="hljs-property">buffer</span> === arrayBuffer);
1106<span class="hljs-comment">// Prints: true</span></code></pre>
1107<h4><code>buf.byteOffset</code><span><a class="mark" href="#buffer_buf_byteoffset" id="buffer_buf_byteoffset">#</a></span></h4>
1108<ul>
1109<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>
1110</ul>
1111<p>When setting <code>byteOffset</code> in <code>Buffer.from(ArrayBuffer, byteOffset, length)</code>,
1112or sometimes when allocating a <code>Buffer</code> smaller than <code>Buffer.poolSize</code>, the
1113buffer does not start from a zero offset on the underlying <code>ArrayBuffer</code>.</p>
1114<p>This can cause problems when accessing the underlying <code>ArrayBuffer</code> directly
1115using <code>buf.buffer</code>, as other parts of the <code>ArrayBuffer</code> may be unrelated
1116to the <code>Buffer</code> object itself.</p>
1117<p>A common issue when creating a <code>TypedArray</code> object that shares its memory with
1118a <code>Buffer</code> is that in this case one needs to specify the <code>byteOffset</code> correctly:</p>
1119<pre><code class="language-js"><span class="hljs-comment">// Create a buffer smaller than `Buffer.poolSize`.</span>
1120<span class="hljs-keyword">const</span> nodeBuffer = <span class="hljs-keyword">new</span> <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>]);
1121
1122<span class="hljs-comment">// When casting the Node.js Buffer to an Int8Array, use the byteOffset</span>
1123<span class="hljs-comment">// to refer only to the part of `nodeBuffer.buffer` that contains the memory</span>
1124<span class="hljs-comment">// for `nodeBuffer`.</span>
1125<span class="hljs-keyword">new</span> <span class="hljs-title class_">Int</span>8<span class="hljs-built_in">Array</span>(nodeBuffer.<span class="hljs-property">buffer</span>, nodeBuffer.<span class="hljs-property">byteOffset</span>, nodeBuffer.<span class="hljs-property">length</span>);</code></pre>
1126<h4><code>buf.compare(target[, targetStart[, targetEnd[, sourceStart[, sourceEnd]]]])</code><span><a class="mark" href="#buffer_buf_compare_target_targetstart_targetend_sourcestart_sourceend" id="buffer_buf_compare_target_targetstart_targetend_sourcestart_sourceend">#</a></span></h4>
1127<div class="api_metadata">
1128<details class="changelog"><summary>History</summary>
1129<table>
1130<tbody><tr><th>Version</th><th>Changes</th></tr>
1131<tr><td>v8.0.0</td>
1132<td><p>The <code>target</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
1133<tr><td>v5.11.0</td>
1134<td><p>Additional parameters for specifying offsets are supported now.</p></td></tr>
1135<tr><td>v0.11.13</td>
1136<td><p><span>Added in: v0.11.13</span></p></td></tr>
1137</tbody></table>
1138</details>
1139</div>
1140<ul>
1141<li><code>target</code> <a href="buffer.html#buffer_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
1142compare <code>buf</code>.</li>
1143<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
1144comparison. <strong>Default:</strong> <code>0</code>.</li>
1145<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
1146(not inclusive). <strong>Default:</strong> <code>target.length</code>.</li>
1147<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.
1148<strong>Default:</strong> <code>0</code>.</li>
1149<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
1150(not inclusive). <strong>Default:</strong> <a href="#buffer_buf_length"><code>buf.length</code></a>.</li>
1151<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1152</ul>
1153<p>Compares <code>buf</code> with <code>target</code> and returns a number indicating whether <code>buf</code>
1154comes before, after, or is the same as <code>target</code> in sort order.
1155Comparison is based on the actual sequence of bytes in each <code>Buffer</code>.</p>
1156<ul>
1157<li><code>0</code> is returned if <code>target</code> is the same as <code>buf</code></li>
1158<li><code>1</code> is returned if <code>target</code> should come <em>before</em> <code>buf</code> when sorted.</li>
1159<li><code>-1</code> is returned if <code>target</code> should come <em>after</em> <code>buf</code> when sorted.</li>
1160</ul>
1161<pre><code class="language-js"><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>);
1162<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>);
1163<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>);
1164
1165<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf1));
1166<span class="hljs-comment">// Prints: 0</span>
1167<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf2));
1168<span class="hljs-comment">// Prints: -1</span>
1169<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">compare</span>(buf3));
1170<span class="hljs-comment">// Prints: -1</span>
1171<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">compare</span>(buf1));
1172<span class="hljs-comment">// Prints: 1</span>
1173<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf2.<span class="hljs-title function_">compare</span>(buf3));
1174<span class="hljs-comment">// Prints: 1</span>
1175<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>));
1176<span class="hljs-comment">// Prints: [ &#x3C;Buffer 41 42 43>, &#x3C;Buffer 41 42 43 44>, &#x3C;Buffer 42 43 44> ]</span>
1177<span class="hljs-comment">// (This result is equal to: [buf1, buf3, buf2].)</span></code></pre>
1178<p>The optional <code>targetStart</code>, <code>targetEnd</code>, <code>sourceStart</code>, and <code>sourceEnd</code>
1179arguments can be used to limit the comparison to specific ranges within <code>target</code>
1180and <code>buf</code> respectively.</p>
1181<pre><code class="language-js"><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>]);
1182<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>]);
1183
1184<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>));
1185<span class="hljs-comment">// Prints: 0</span>
1186<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>));
1187<span class="hljs-comment">// Prints: -1</span>
1188<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>));
1189<span class="hljs-comment">// Prints: 1</span></code></pre>
1190<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>,
1191<code>targetEnd > target.byteLength</code>, or <code>sourceEnd > source.byteLength</code>.</p>
1192<h4><code>buf.copy(target[, targetStart[, sourceStart[, sourceEnd]]])</code><span><a class="mark" href="#buffer_buf_copy_target_targetstart_sourcestart_sourceend" id="buffer_buf_copy_target_targetstart_sourcestart_sourceend">#</a></span></h4>
1193<div class="api_metadata">
1194<span>Added in: v0.1.90</span>
1195</div>
1196<ul>
1197<li><code>target</code> <a href="buffer.html#buffer_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>
1198<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
1199writing. <strong>Default:</strong> <code>0</code>.</li>
1200<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.
1201<strong>Default:</strong> <code>0</code>.</li>
1202<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
1203inclusive). <strong>Default:</strong> <a href="#buffer_buf_length"><code>buf.length</code></a>.</li>
1204<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>
1205</ul>
1206<p>Copies data from a region of <code>buf</code> to a region in <code>target</code>, even if the <code>target</code>
1207memory region overlaps with <code>buf</code>.</p>
1208<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
1209for all TypedArrays, including Node.js <code>Buffer</code>s, although it takes
1210different function arguments.</p>
1211<pre><code class="language-js"><span class="hljs-comment">// Create two `Buffer` instances.</span>
1212<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>);
1213<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>);
1214
1215<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++) {
1216  <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
1217  buf1[i] = i + <span class="hljs-number">97</span>;
1218}
1219
1220<span class="hljs-comment">// Copy `buf1` bytes 16 through 19 into `buf2` starting at byte 8 of `buf2`.</span>
1221buf1.<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>);
1222<span class="hljs-comment">// This is equivalent to:</span>
1223<span class="hljs-comment">// buf2.set(buf1.subarray(16, 20), 8);</span>
1224
1225<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>));
1226<span class="hljs-comment">// Prints: !!!!!!!!qrst!!!!!!!!!!!!!</span></code></pre>
1227<pre><code class="language-js"><span class="hljs-comment">// Create a `Buffer` and copy data from one region to an overlapping region</span>
1228<span class="hljs-comment">// within the same `Buffer`.</span>
1229
1230<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>);
1231
1232<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++) {
1233  <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
1234  buf[i] = i + <span class="hljs-number">97</span>;
1235}
1236
1237buf.<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>);
1238
1239<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>());
1240<span class="hljs-comment">// Prints: efghijghijklmnopqrstuvwxyz</span></code></pre>
1241<h4><code>buf.entries()</code><span><a class="mark" href="#buffer_buf_entries" id="buffer_buf_entries">#</a></span></h4>
1242<div class="api_metadata">
1243<span>Added in: v1.1.0</span>
1244</div>
1245<ul>
1246<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>
1247</ul>
1248<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
1249of <code>buf</code>.</p>
1250<pre><code class="language-js"><span class="hljs-comment">// Log the entire contents of a `Buffer`.</span>
1251
1252<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>);
1253
1254<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>()) {
1255  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(pair);
1256}
1257<span class="hljs-comment">// Prints:</span>
1258<span class="hljs-comment">//   [0, 98]</span>
1259<span class="hljs-comment">//   [1, 117]</span>
1260<span class="hljs-comment">//   [2, 102]</span>
1261<span class="hljs-comment">//   [3, 102]</span>
1262<span class="hljs-comment">//   [4, 101]</span>
1263<span class="hljs-comment">//   [5, 114]</span></code></pre>
1264<h4><code>buf.equals(otherBuffer)</code><span><a class="mark" href="#buffer_buf_equals_otherbuffer" id="buffer_buf_equals_otherbuffer">#</a></span></h4>
1265<div class="api_metadata">
1266<details class="changelog"><summary>History</summary>
1267<table>
1268<tbody><tr><th>Version</th><th>Changes</th></tr>
1269<tr><td>v8.0.0</td>
1270<td><p>The arguments can now be <code>Uint8Array</code>s.</p></td></tr>
1271<tr><td>v0.11.13</td>
1272<td><p><span>Added in: v0.11.13</span></p></td></tr>
1273</tbody></table>
1274</details>
1275</div>
1276<ul>
1277<li><code>otherBuffer</code> <a href="buffer.html#buffer_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
1278compare <code>buf</code>.</li>
1279<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
1280</ul>
1281<p>Returns <code>true</code> if both <code>buf</code> and <code>otherBuffer</code> have exactly the same bytes,
1282<code>false</code> otherwise. Equivalent to
1283<a href="#buffer_buf_compare_target_targetstart_targetend_sourcestart_sourceend"><code>buf.compare(otherBuffer) === 0</code></a>.</p>
1284<pre><code class="language-js"><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>);
1285<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>);
1286<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>);
1287
1288<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">equals</span>(buf2));
1289<span class="hljs-comment">// Prints: true</span>
1290<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1.<span class="hljs-title function_">equals</span>(buf3));
1291<span class="hljs-comment">// Prints: false</span></code></pre>
1292<h4><code>buf.fill(value[, offset[, end]][, encoding])</code><span><a class="mark" href="#buffer_buf_fill_value_offset_end_encoding" id="buffer_buf_fill_value_offset_end_encoding">#</a></span></h4>
1293<div class="api_metadata">
1294<details class="changelog"><summary>History</summary>
1295<table>
1296<tbody><tr><th>Version</th><th>Changes</th></tr>
1297<tr><td>v11.0.0</td>
1298<td><p>Throws <code>ERR_OUT_OF_RANGE</code> instead of <code>ERR_INDEX_OUT_OF_RANGE</code>.</p></td></tr>
1299<tr><td>v10.0.0</td>
1300<td><p>Negative <code>end</code> values throw an <code>ERR_INDEX_OUT_OF_RANGE</code> error.</p></td></tr>
1301<tr><td>v10.0.0</td>
1302<td><p>Attempting to fill a non-zero length buffer with a zero length buffer triggers a thrown exception.</p></td></tr>
1303<tr><td>v10.0.0</td>
1304<td><p>Specifying an invalid string for <code>value</code> triggers a thrown exception.</p></td></tr>
1305<tr><td>v5.7.0</td>
1306<td><p>The <code>encoding</code> parameter is supported now.</p></td></tr>
1307<tr><td>v0.5.0</td>
1308<td><p><span>Added in: v0.5.0</span></p></td></tr>
1309</tbody></table>
1310</details>
1311</div>
1312<ul>
1313<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#buffer_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>.</li>
1314<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>.
1315<strong>Default:</strong> <code>0</code>.</li>
1316<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>
1317<a href="#buffer_buf_length"><code>buf.length</code></a>.</li>
1318<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.
1319<strong>Default:</strong> <code>'utf8'</code>.</li>
1320<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> A reference to <code>buf</code>.</li>
1321</ul>
1322<p>Fills <code>buf</code> with the specified <code>value</code>. If the <code>offset</code> and <code>end</code> are not given,
1323the entire <code>buf</code> will be filled:</p>
1324<pre><code class="language-js"><span class="hljs-comment">// Fill a `Buffer` with the ASCII character 'h'.</span>
1325
1326<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>);
1327
1328<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b.<span class="hljs-title function_">toString</span>());
1329<span class="hljs-comment">// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh</span></code></pre>
1330<p><code>value</code> is coerced to a <code>uint32</code> value if it is not a string, <code>Buffer</code>, or
1331integer. If the resulting integer is greater than <code>255</code> (decimal), <code>buf</code> will be
1332filled with <code>value &#x26; 255</code>.</p>
1333<p>If the final write of a <code>fill()</code> operation falls on a multi-byte character,
1334then only the bytes of that character that fit into <code>buf</code> are written:</p>
1335<pre><code class="language-js"><span class="hljs-comment">// Fill a `Buffer` with character that takes up two bytes in UTF-8.</span>
1336
1337<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>));
1338<span class="hljs-comment">// Prints: &#x3C;Buffer c8 a2 c8 a2 c8></span></code></pre>
1339<p>If <code>value</code> contains invalid characters, it is truncated; if no valid
1340fill data remains, an exception is thrown:</p>
1341<pre><code class="language-js"><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>);
1342
1343<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>));
1344<span class="hljs-comment">// Prints: &#x3C;Buffer 61 61 61 61 61></span>
1345<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>));
1346<span class="hljs-comment">// Prints: &#x3C;Buffer aa aa aa aa aa></span>
1347<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>));
1348<span class="hljs-comment">// Throws an exception.</span></code></pre>
1349<h4><code>buf.includes(value[, byteOffset][, encoding])</code><span><a class="mark" href="#buffer_buf_includes_value_byteoffset_encoding" id="buffer_buf_includes_value_byteoffset_encoding">#</a></span></h4>
1350<div class="api_metadata">
1351<span>Added in: v5.3.0</span>
1352</div>
1353<ul>
1354<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#buffer_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>
1355<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
1356offset is calculated from the end of <code>buf</code>. <strong>Default:</strong> <code>0</code>.</li>
1357<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.
1358<strong>Default:</strong> <code>'utf8'</code>.</li>
1359<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>
1360</ul>
1361<p>Equivalent to <a href="#buffer_buf_indexof_value_byteoffset_encoding"><code>buf.indexOf() !== -1</code></a>.</p>
1362<pre><code class="language-js"><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>);
1363
1364<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>));
1365<span class="hljs-comment">// Prints: true</span>
1366<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>));
1367<span class="hljs-comment">// Prints: true</span>
1368<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>)));
1369<span class="hljs-comment">// Prints: true</span>
1370<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>));
1371<span class="hljs-comment">// Prints: true (97 is the decimal ASCII value for 'a')</span>
1372<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>)));
1373<span class="hljs-comment">// Prints: false</span>
1374<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>)));
1375<span class="hljs-comment">// Prints: true</span>
1376<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>));
1377<span class="hljs-comment">// Prints: false</span></code></pre>
1378<h4><code>buf.indexOf(value[, byteOffset][, encoding])</code><span><a class="mark" href="#buffer_buf_indexof_value_byteoffset_encoding" id="buffer_buf_indexof_value_byteoffset_encoding">#</a></span></h4>
1379<div class="api_metadata">
1380<details class="changelog"><summary>History</summary>
1381<table>
1382<tbody><tr><th>Version</th><th>Changes</th></tr>
1383<tr><td>v8.0.0</td>
1384<td><p>The <code>value</code> can now be a <code>Uint8Array</code>.</p></td></tr>
1385<tr><td>v5.7.0, v4.4.0</td>
1386<td><p>When <code>encoding</code> is being passed, the <code>byteOffset</code> parameter is no longer required.</p></td></tr>
1387<tr><td>v1.5.0</td>
1388<td><p><span>Added in: v1.5.0</span></p></td></tr>
1389</tbody></table>
1390</details>
1391</div>
1392<ul>
1393<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#buffer_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>
1394<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
1395offset is calculated from the end of <code>buf</code>. <strong>Default:</strong> <code>0</code>.</li>
1396<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
1397determine the binary representation of the string that will be searched for in
1398<code>buf</code>. <strong>Default:</strong> <code>'utf8'</code>.</li>
1399<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
1400<code>-1</code> if <code>buf</code> does not contain <code>value</code>.</li>
1401</ul>
1402<p>If <code>value</code> is:</p>
1403<ul>
1404<li>a string, <code>value</code> is interpreted according to the character encoding in
1405<code>encoding</code>.</li>
1406<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.
1407To compare a partial <code>Buffer</code>, use <a href="#buffer_buf_slice_start_end"><code>buf.slice()</code></a>.</li>
1408<li>a number, <code>value</code> will be interpreted as an unsigned 8-bit integer
1409value between <code>0</code> and <code>255</code>.</li>
1410</ul>
1411<pre><code class="language-js"><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>);
1412
1413<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>));
1414<span class="hljs-comment">// Prints: 0</span>
1415<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>));
1416<span class="hljs-comment">// Prints: 2</span>
1417<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>)));
1418<span class="hljs-comment">// Prints: 8</span>
1419<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>));
1420<span class="hljs-comment">// Prints: 8 (97 is the decimal ASCII value for 'a')</span>
1421<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>)));
1422<span class="hljs-comment">// Prints: -1</span>
1423<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>)));
1424<span class="hljs-comment">// Prints: 8</span>
1425
1426<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>);
1427
1428<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>));
1429<span class="hljs-comment">// Prints: 4</span>
1430<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>));
1431<span class="hljs-comment">// Prints: 6</span></code></pre>
1432<p>If <code>value</code> is not a string, number, or <code>Buffer</code>, this method will throw a
1433<code>TypeError</code>. If <code>value</code> is a number, it will be coerced to a valid byte value,
1434an integer between 0 and 255.</p>
1435<p>If <code>byteOffset</code> is not a number, it will be coerced to a number. If the result
1436of coercion is <code>NaN</code> or <code>0</code>, then the entire buffer will be searched. This
1437behavior matches <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf"><code>String.prototype.indexOf()</code></a>.</p>
1438<pre><code class="language-js"><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>);
1439
1440<span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span>
1441<span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span>
1442<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>));
1443<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>));
1444
1445<span class="hljs-comment">// Passing a byteOffset that coerces to NaN or 0.</span>
1446<span class="hljs-comment">// Prints: 1, searching the whole buffer.</span>
1447<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>));
1448<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>, {}));
1449<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>));
1450<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></pre>
1451<p>If <code>value</code> is an empty string or empty <code>Buffer</code> and <code>byteOffset</code> is less
1452than <code>buf.length</code>, <code>byteOffset</code> will be returned. If <code>value</code> is empty and
1453<code>byteOffset</code> is at least <code>buf.length</code>, <code>buf.length</code> will be returned.</p>
1454<h4><code>buf.keys()</code><span><a class="mark" href="#buffer_buf_keys" id="buffer_buf_keys">#</a></span></h4>
1455<div class="api_metadata">
1456<span>Added in: v1.1.0</span>
1457</div>
1458<ul>
1459<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>
1460</ul>
1461<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>
1462<pre><code class="language-js"><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>);
1463
1464<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>()) {
1465  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(key);
1466}
1467<span class="hljs-comment">// Prints:</span>
1468<span class="hljs-comment">//   0</span>
1469<span class="hljs-comment">//   1</span>
1470<span class="hljs-comment">//   2</span>
1471<span class="hljs-comment">//   3</span>
1472<span class="hljs-comment">//   4</span>
1473<span class="hljs-comment">//   5</span></code></pre>
1474<h4><code>buf.lastIndexOf(value[, byteOffset][, encoding])</code><span><a class="mark" href="#buffer_buf_lastindexof_value_byteoffset_encoding" id="buffer_buf_lastindexof_value_byteoffset_encoding">#</a></span></h4>
1475<div class="api_metadata">
1476<details class="changelog"><summary>History</summary>
1477<table>
1478<tbody><tr><th>Version</th><th>Changes</th></tr>
1479<tr><td>v8.0.0</td>
1480<td><p>The <code>value</code> can now be a <code>Uint8Array</code>.</p></td></tr>
1481<tr><td>v6.0.0</td>
1482<td><p><span>Added in: v6.0.0</span></p></td></tr>
1483</tbody></table>
1484</details>
1485</div>
1486<ul>
1487<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#buffer_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>
1488<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
1489offset is calculated from the end of <code>buf</code>. <strong>Default:</strong>
1490<code>buf.length - 1</code>.</li>
1491<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
1492determine the binary representation of the string that will be searched for in
1493<code>buf</code>. <strong>Default:</strong> <code>'utf8'</code>.</li>
1494<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
1495<code>-1</code> if <code>buf</code> does not contain <code>value</code>.</li>
1496</ul>
1497<p>Identical to <a href="#buffer_buf_indexof_value_byteoffset_encoding"><code>buf.indexOf()</code></a>, except the last occurrence of <code>value</code> is found
1498rather than the first occurrence.</p>
1499<pre><code class="language-js"><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>);
1500
1501<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>));
1502<span class="hljs-comment">// Prints: 0</span>
1503<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>));
1504<span class="hljs-comment">// Prints: 17</span>
1505<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>)));
1506<span class="hljs-comment">// Prints: 17</span>
1507<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>));
1508<span class="hljs-comment">// Prints: 15 (97 is the decimal ASCII value for 'a')</span>
1509<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>)));
1510<span class="hljs-comment">// Prints: -1</span>
1511<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>));
1512<span class="hljs-comment">// Prints: 5</span>
1513<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>));
1514<span class="hljs-comment">// Prints: -1</span>
1515
1516<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>);
1517
1518<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>));
1519<span class="hljs-comment">// Prints: 6</span>
1520<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>));
1521<span class="hljs-comment">// Prints: 4</span></code></pre>
1522<p>If <code>value</code> is not a string, number, or <code>Buffer</code>, this method will throw a
1523<code>TypeError</code>. If <code>value</code> is a number, it will be coerced to a valid byte value,
1524an integer between 0 and 255.</p>
1525<p>If <code>byteOffset</code> is not a number, it will be coerced to a number. Any arguments
1526that coerce to <code>NaN</code>, like <code>{}</code> or <code>undefined</code>, will search the whole buffer.
1527This 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>
1528<pre><code class="language-js"><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>);
1529
1530<span class="hljs-comment">// Passing a value that's a number, but not a valid byte.</span>
1531<span class="hljs-comment">// Prints: 2, equivalent to searching for 99 or 'c'.</span>
1532<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>));
1533<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>));
1534
1535<span class="hljs-comment">// Passing a byteOffset that coerces to NaN.</span>
1536<span class="hljs-comment">// Prints: 1, searching the whole buffer.</span>
1537<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>));
1538<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>, {}));
1539
1540<span class="hljs-comment">// Passing a byteOffset that coerces to 0.</span>
1541<span class="hljs-comment">// Prints: -1, equivalent to passing 0.</span>
1542<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>));
1543<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></pre>
1544<p>If <code>value</code> is an empty string or empty <code>Buffer</code>, <code>byteOffset</code> will be returned.</p>
1545<h4><code>buf.length</code><span><a class="mark" href="#buffer_buf_length" id="buffer_buf_length">#</a></span></h4>
1546<div class="api_metadata">
1547<span>Added in: v0.1.90</span>
1548</div>
1549<ul>
1550<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1551</ul>
1552<p>Returns the number of bytes in <code>buf</code>.</p>
1553<pre><code class="language-js"><span class="hljs-comment">// Create a `Buffer` and write a shorter string to it using UTF-8.</span>
1554
1555<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>);
1556
1557<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>);
1558<span class="hljs-comment">// Prints: 1234</span>
1559
1560buf.<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>);
1561
1562<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-property">length</span>);
1563<span class="hljs-comment">// Prints: 1234</span></code></pre>
1564<h4><code>buf.parent</code><span><a class="mark" href="#buffer_buf_parent" id="buffer_buf_parent">#</a></span></h4>
1565<div class="api_metadata">
1566<span>Deprecated since: v8.0.0</span>
1567</div>
1568<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#buffer_buf_buffer"><code>buf.buffer</code></a> instead.</div><p></p>
1569<p>The <code>buf.parent</code> property is a deprecated alias for <code>buf.buffer</code>.</p>
1570<h4><code>buf.readBigInt64BE([offset])</code><span><a class="mark" href="#buffer_buf_readbigint64be_offset" id="buffer_buf_readbigint64be_offset">#</a></span></h4>
1571<div class="api_metadata">
1572<span>Added in: v12.0.0, v10.20.0</span>
1573</div>
1574<ul>
1575<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
1576satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
1577<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
1578</ul>
1579<p>Reads a signed, big-endian 64-bit integer from <code>buf</code> at the specified <code>offset</code>.</p>
1580<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed
1581values.</p>
1582<h4><code>buf.readBigInt64LE([offset])</code><span><a class="mark" href="#buffer_buf_readbigint64le_offset" id="buffer_buf_readbigint64le_offset">#</a></span></h4>
1583<div class="api_metadata">
1584<span>Added in: v12.0.0, v10.20.0</span>
1585</div>
1586<ul>
1587<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
1588satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
1589<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
1590</ul>
1591<p>Reads a signed, little-endian 64-bit integer from <code>buf</code> at the specified
1592<code>offset</code>.</p>
1593<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed
1594values.</p>
1595<h4><code>buf.readBigUInt64BE([offset])</code><span><a class="mark" href="#buffer_buf_readbiguint64be_offset" id="buffer_buf_readbiguint64be_offset">#</a></span></h4>
1596<div class="api_metadata">
1597<details class="changelog"><summary>History</summary>
1598<table>
1599<tbody><tr><th>Version</th><th>Changes</th></tr>
1600<tr><td>v14.10.0</td>
1601<td><p>This function is also available as <code>buf.readBigUint64BE()</code>.</p></td></tr>
1602<tr><td>v12.0.0, v10.20.0</td>
1603<td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr>
1604</tbody></table>
1605</details>
1606</div>
1607<ul>
1608<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
1609satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
1610<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
1611</ul>
1612<p>Reads an unsigned, big-endian 64-bit integer from <code>buf</code> at the specified
1613<code>offset</code>.</p>
1614<p>This function is also available under the <code>readBigUint64BE</code> alias.</p>
1615<pre><code class="language-js"><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>]);
1616
1617<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>));
1618<span class="hljs-comment">// Prints: 4294967295n</span></code></pre>
1619<h4><code>buf.readBigUInt64LE([offset])</code><span><a class="mark" href="#buffer_buf_readbiguint64le_offset" id="buffer_buf_readbiguint64le_offset">#</a></span></h4>
1620<div class="api_metadata">
1621<details class="changelog"><summary>History</summary>
1622<table>
1623<tbody><tr><th>Version</th><th>Changes</th></tr>
1624<tr><td>v14.10.0, v12.19.0</td>
1625<td><p>This function is also available as <code>buf.readBigUint64LE()</code>.</p></td></tr>
1626<tr><td>v12.0.0, v10.20.0</td>
1627<td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr>
1628</tbody></table>
1629</details>
1630</div>
1631<ul>
1632<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
1633satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
1634<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt" class="type">&#x3C;bigint></a></li>
1635</ul>
1636<p>Reads an unsigned, little-endian 64-bit integer from <code>buf</code> at the specified
1637<code>offset</code>.</p>
1638<p>This function is also available under the <code>readBigUint64LE</code> alias.</p>
1639<pre><code class="language-js"><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>]);
1640
1641<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>));
1642<span class="hljs-comment">// Prints: 18446744069414584320n</span></code></pre>
1643<h4><code>buf.readDoubleBE([offset])</code><span><a class="mark" href="#buffer_buf_readdoublebe_offset" id="buffer_buf_readdoublebe_offset">#</a></span></h4>
1644<div class="api_metadata">
1645<details class="changelog"><summary>History</summary>
1646<table>
1647<tbody><tr><th>Version</th><th>Changes</th></tr>
1648<tr><td>v10.0.0</td>
1649<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
1650<tr><td>v0.11.15</td>
1651<td><p><span>Added in: v0.11.15</span></p></td></tr>
1652</tbody></table>
1653</details>
1654</div>
1655<ul>
1656<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
1657satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
1658<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1659</ul>
1660<p>Reads a 64-bit, big-endian double from <code>buf</code> at the specified <code>offset</code>.</p>
1661<pre><code class="language-js"><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>]);
1662
1663<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>));
1664<span class="hljs-comment">// Prints: 8.20788039913184e-304</span></code></pre>
1665<h4><code>buf.readDoubleLE([offset])</code><span><a class="mark" href="#buffer_buf_readdoublele_offset" id="buffer_buf_readdoublele_offset">#</a></span></h4>
1666<div class="api_metadata">
1667<details class="changelog"><summary>History</summary>
1668<table>
1669<tbody><tr><th>Version</th><th>Changes</th></tr>
1670<tr><td>v10.0.0</td>
1671<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
1672<tr><td>v0.11.15</td>
1673<td><p><span>Added in: v0.11.15</span></p></td></tr>
1674</tbody></table>
1675</details>
1676</div>
1677<ul>
1678<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
1679satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
1680<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1681</ul>
1682<p>Reads a 64-bit, little-endian double from <code>buf</code> at the specified <code>offset</code>.</p>
1683<pre><code class="language-js"><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>]);
1684
1685<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>));
1686<span class="hljs-comment">// Prints: 5.447603722011605e-270</span>
1687<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>));
1688<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
1689<h4><code>buf.readFloatBE([offset])</code><span><a class="mark" href="#buffer_buf_readfloatbe_offset" id="buffer_buf_readfloatbe_offset">#</a></span></h4>
1690<div class="api_metadata">
1691<details class="changelog"><summary>History</summary>
1692<table>
1693<tbody><tr><th>Version</th><th>Changes</th></tr>
1694<tr><td>v10.0.0</td>
1695<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
1696<tr><td>v0.11.15</td>
1697<td><p><span>Added in: v0.11.15</span></p></td></tr>
1698</tbody></table>
1699</details>
1700</div>
1701<ul>
1702<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
1703satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
1704<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1705</ul>
1706<p>Reads a 32-bit, big-endian float from <code>buf</code> at the specified <code>offset</code>.</p>
1707<pre><code class="language-js"><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>]);
1708
1709<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>));
1710<span class="hljs-comment">// Prints: 2.387939260590663e-38</span></code></pre>
1711<h4><code>buf.readFloatLE([offset])</code><span><a class="mark" href="#buffer_buf_readfloatle_offset" id="buffer_buf_readfloatle_offset">#</a></span></h4>
1712<div class="api_metadata">
1713<details class="changelog"><summary>History</summary>
1714<table>
1715<tbody><tr><th>Version</th><th>Changes</th></tr>
1716<tr><td>v10.0.0</td>
1717<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
1718<tr><td>v0.11.15</td>
1719<td><p><span>Added in: v0.11.15</span></p></td></tr>
1720</tbody></table>
1721</details>
1722</div>
1723<ul>
1724<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
1725satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
1726<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
1727</ul>
1728<p>Reads a 32-bit, little-endian float from <code>buf</code> at the specified <code>offset</code>.</p>
1729<pre><code class="language-js"><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>]);
1730
1731<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>));
1732<span class="hljs-comment">// Prints: 1.539989614439558e-36</span>
1733<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>));
1734<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
1735<h4><code>buf.readInt8([offset])</code><span><a class="mark" href="#buffer_buf_readint8_offset" id="buffer_buf_readint8_offset">#</a></span></h4>
1736<div class="api_metadata">
1737<details class="changelog"><summary>History</summary>
1738<table>
1739<tbody><tr><th>Version</th><th>Changes</th></tr>
1740<tr><td>v10.0.0</td>
1741<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
1742<tr><td>v0.5.0</td>
1743<td><p><span>Added in: v0.5.0</span></p></td></tr>
1744</tbody></table>
1745</details>
1746</div>
1747<ul>
1748<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
1749satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li>
1750<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1751</ul>
1752<p>Reads a signed 8-bit integer from <code>buf</code> at the specified <code>offset</code>.</p>
1753<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p>
1754<pre><code class="language-js"><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>]);
1755
1756<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>));
1757<span class="hljs-comment">// Prints: -1</span>
1758<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>));
1759<span class="hljs-comment">// Prints: 5</span>
1760<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>));
1761<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
1762<h4><code>buf.readInt16BE([offset])</code><span><a class="mark" href="#buffer_buf_readint16be_offset" id="buffer_buf_readint16be_offset">#</a></span></h4>
1763<div class="api_metadata">
1764<details class="changelog"><summary>History</summary>
1765<table>
1766<tbody><tr><th>Version</th><th>Changes</th></tr>
1767<tr><td>v10.0.0</td>
1768<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
1769<tr><td>v0.5.5</td>
1770<td><p><span>Added in: v0.5.5</span></p></td></tr>
1771</tbody></table>
1772</details>
1773</div>
1774<ul>
1775<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
1776satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li>
1777<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1778</ul>
1779<p>Reads a signed, big-endian 16-bit integer from <code>buf</code> at the specified <code>offset</code>.</p>
1780<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p>
1781<pre><code class="language-js"><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>]);
1782
1783<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>));
1784<span class="hljs-comment">// Prints: 5</span></code></pre>
1785<h4><code>buf.readInt16LE([offset])</code><span><a class="mark" href="#buffer_buf_readint16le_offset" id="buffer_buf_readint16le_offset">#</a></span></h4>
1786<div class="api_metadata">
1787<details class="changelog"><summary>History</summary>
1788<table>
1789<tbody><tr><th>Version</th><th>Changes</th></tr>
1790<tr><td>v10.0.0</td>
1791<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
1792<tr><td>v0.5.5</td>
1793<td><p><span>Added in: v0.5.5</span></p></td></tr>
1794</tbody></table>
1795</details>
1796</div>
1797<ul>
1798<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
1799satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li>
1800<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1801</ul>
1802<p>Reads a signed, little-endian 16-bit integer from <code>buf</code> at the specified
1803<code>offset</code>.</p>
1804<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p>
1805<pre><code class="language-js"><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>]);
1806
1807<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>));
1808<span class="hljs-comment">// Prints: 1280</span>
1809<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>));
1810<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
1811<h4><code>buf.readInt32BE([offset])</code><span><a class="mark" href="#buffer_buf_readint32be_offset" id="buffer_buf_readint32be_offset">#</a></span></h4>
1812<div class="api_metadata">
1813<details class="changelog"><summary>History</summary>
1814<table>
1815<tbody><tr><th>Version</th><th>Changes</th></tr>
1816<tr><td>v10.0.0</td>
1817<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
1818<tr><td>v0.5.5</td>
1819<td><p><span>Added in: v0.5.5</span></p></td></tr>
1820</tbody></table>
1821</details>
1822</div>
1823<ul>
1824<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
1825satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
1826<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1827</ul>
1828<p>Reads a signed, big-endian 32-bit integer from <code>buf</code> at the specified <code>offset</code>.</p>
1829<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p>
1830<pre><code class="language-js"><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>]);
1831
1832<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>));
1833<span class="hljs-comment">// Prints: 5</span></code></pre>
1834<h4><code>buf.readInt32LE([offset])</code><span><a class="mark" href="#buffer_buf_readint32le_offset" id="buffer_buf_readint32le_offset">#</a></span></h4>
1835<div class="api_metadata">
1836<details class="changelog"><summary>History</summary>
1837<table>
1838<tbody><tr><th>Version</th><th>Changes</th></tr>
1839<tr><td>v10.0.0</td>
1840<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
1841<tr><td>v0.5.5</td>
1842<td><p><span>Added in: v0.5.5</span></p></td></tr>
1843</tbody></table>
1844</details>
1845</div>
1846<ul>
1847<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
1848satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
1849<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1850</ul>
1851<p>Reads a signed, little-endian 32-bit integer from <code>buf</code> at the specified
1852<code>offset</code>.</p>
1853<p>Integers read from a <code>Buffer</code> are interpreted as two's complement signed values.</p>
1854<pre><code class="language-js"><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>]);
1855
1856<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>));
1857<span class="hljs-comment">// Prints: 83886080</span>
1858<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>));
1859<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
1860<h4><code>buf.readIntBE(offset, byteLength)</code><span><a class="mark" href="#buffer_buf_readintbe_offset_bytelength" id="buffer_buf_readintbe_offset_bytelength">#</a></span></h4>
1861<div class="api_metadata">
1862<details class="changelog"><summary>History</summary>
1863<table>
1864<tbody><tr><th>Version</th><th>Changes</th></tr>
1865<tr><td>v10.0.0</td>
1866<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>
1867<tr><td>v0.11.15</td>
1868<td><p><span>Added in: v0.11.15</span></p></td></tr>
1869</tbody></table>
1870</details>
1871</div>
1872<ul>
1873<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
1874satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
1875<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
1876<code>0 &#x3C; byteLength &#x3C;= 6</code>.</li>
1877<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1878</ul>
1879<p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code>
1880and interprets the result as a big-endian, two's complement signed value
1881supporting up to 48 bits of accuracy.</p>
1882<pre><code class="language-js"><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>]);
1883
1884<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>));
1885<span class="hljs-comment">// Prints: 1234567890ab</span>
1886<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>));
1887<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span>
1888<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>));
1889<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
1890<h4><code>buf.readIntLE(offset, byteLength)</code><span><a class="mark" href="#buffer_buf_readintle_offset_bytelength" id="buffer_buf_readintle_offset_bytelength">#</a></span></h4>
1891<div class="api_metadata">
1892<details class="changelog"><summary>History</summary>
1893<table>
1894<tbody><tr><th>Version</th><th>Changes</th></tr>
1895<tr><td>v10.0.0</td>
1896<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>
1897<tr><td>v0.11.15</td>
1898<td><p><span>Added in: v0.11.15</span></p></td></tr>
1899</tbody></table>
1900</details>
1901</div>
1902<ul>
1903<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
1904satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
1905<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
1906<code>0 &#x3C; byteLength &#x3C;= 6</code>.</li>
1907<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1908</ul>
1909<p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code>
1910and interprets the result as a little-endian, two's complement signed value
1911supporting up to 48 bits of accuracy.</p>
1912<pre><code class="language-js"><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>]);
1913
1914<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>));
1915<span class="hljs-comment">// Prints: -546f87a9cbee</span></code></pre>
1916<h4><code>buf.readUInt8([offset])</code><span><a class="mark" href="#buffer_buf_readuint8_offset" id="buffer_buf_readuint8_offset">#</a></span></h4>
1917<div class="api_metadata">
1918<details class="changelog"><summary>History</summary>
1919<table>
1920<tbody><tr><th>Version</th><th>Changes</th></tr>
1921<tr><td>v14.9.0</td>
1922<td><p>This function is also available as <code>buf.readUint8()</code>.</p></td></tr>
1923<tr><td>v10.0.0</td>
1924<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
1925<tr><td>v0.5.0</td>
1926<td><p><span>Added in: v0.5.0</span></p></td></tr>
1927</tbody></table>
1928</details>
1929</div>
1930<ul>
1931<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
1932satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li>
1933<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1934</ul>
1935<p>Reads an unsigned 8-bit integer from <code>buf</code> at the specified <code>offset</code>.</p>
1936<p>This function is also available under the <code>readUint8</code> alias.</p>
1937<pre><code class="language-js"><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>]);
1938
1939<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>));
1940<span class="hljs-comment">// Prints: 1</span>
1941<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>));
1942<span class="hljs-comment">// Prints: 254</span>
1943<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>));
1944<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
1945<h4><code>buf.readUInt16BE([offset])</code><span><a class="mark" href="#buffer_buf_readuint16be_offset" id="buffer_buf_readuint16be_offset">#</a></span></h4>
1946<div class="api_metadata">
1947<details class="changelog"><summary>History</summary>
1948<table>
1949<tbody><tr><th>Version</th><th>Changes</th></tr>
1950<tr><td>v14.9.0</td>
1951<td><p>This function is also available as <code>buf.readUint16BE()</code>.</p></td></tr>
1952<tr><td>v10.0.0</td>
1953<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
1954<tr><td>v0.5.5</td>
1955<td><p><span>Added in: v0.5.5</span></p></td></tr>
1956</tbody></table>
1957</details>
1958</div>
1959<ul>
1960<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
1961satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li>
1962<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1963</ul>
1964<p>Reads an unsigned, big-endian 16-bit integer from <code>buf</code> at the specified
1965<code>offset</code>.</p>
1966<p>This function is also available under the <code>readUint16BE</code> alias.</p>
1967<pre><code class="language-js"><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>]);
1968
1969<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>));
1970<span class="hljs-comment">// Prints: 1234</span>
1971<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>));
1972<span class="hljs-comment">// Prints: 3456</span></code></pre>
1973<h4><code>buf.readUInt16LE([offset])</code><span><a class="mark" href="#buffer_buf_readuint16le_offset" id="buffer_buf_readuint16le_offset">#</a></span></h4>
1974<div class="api_metadata">
1975<details class="changelog"><summary>History</summary>
1976<table>
1977<tbody><tr><th>Version</th><th>Changes</th></tr>
1978<tr><td>v14.9.0</td>
1979<td><p>This function is also available as <code>buf.readUint16LE()</code>.</p></td></tr>
1980<tr><td>v10.0.0</td>
1981<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
1982<tr><td>v0.5.5</td>
1983<td><p><span>Added in: v0.5.5</span></p></td></tr>
1984</tbody></table>
1985</details>
1986</div>
1987<ul>
1988<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
1989satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li>
1990<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
1991</ul>
1992<p>Reads an unsigned, little-endian 16-bit integer from <code>buf</code> at the specified
1993<code>offset</code>.</p>
1994<p>This function is also available under the <code>readUint16LE</code> alias.</p>
1995<pre><code class="language-js"><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>]);
1996
1997<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>));
1998<span class="hljs-comment">// Prints: 3412</span>
1999<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>));
2000<span class="hljs-comment">// Prints: 5634</span>
2001<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>));
2002<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
2003<h4><code>buf.readUInt32BE([offset])</code><span><a class="mark" href="#buffer_buf_readuint32be_offset" id="buffer_buf_readuint32be_offset">#</a></span></h4>
2004<div class="api_metadata">
2005<details class="changelog"><summary>History</summary>
2006<table>
2007<tbody><tr><th>Version</th><th>Changes</th></tr>
2008<tr><td>v14.9.0</td>
2009<td><p>This function is also available as <code>buf.readUint32BE()</code>.</p></td></tr>
2010<tr><td>v10.0.0</td>
2011<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2012<tr><td>v0.5.5</td>
2013<td><p><span>Added in: v0.5.5</span></p></td></tr>
2014</tbody></table>
2015</details>
2016</div>
2017<ul>
2018<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
2019satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
2020<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
2021</ul>
2022<p>Reads an unsigned, big-endian 32-bit integer from <code>buf</code> at the specified
2023<code>offset</code>.</p>
2024<p>This function is also available under the <code>readUint32BE</code> alias.</p>
2025<pre><code class="language-js"><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>]);
2026
2027<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>));
2028<span class="hljs-comment">// Prints: 12345678</span></code></pre>
2029<h4><code>buf.readUInt32LE([offset])</code><span><a class="mark" href="#buffer_buf_readuint32le_offset" id="buffer_buf_readuint32le_offset">#</a></span></h4>
2030<div class="api_metadata">
2031<details class="changelog"><summary>History</summary>
2032<table>
2033<tbody><tr><th>Version</th><th>Changes</th></tr>
2034<tr><td>v14.9.0</td>
2035<td><p>This function is also available as <code>buf.readUint32LE()</code>.</p></td></tr>
2036<tr><td>v10.0.0</td>
2037<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2038<tr><td>v0.5.5</td>
2039<td><p><span>Added in: v0.5.5</span></p></td></tr>
2040</tbody></table>
2041</details>
2042</div>
2043<ul>
2044<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
2045satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
2046<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
2047</ul>
2048<p>Reads an unsigned, little-endian 32-bit integer from <code>buf</code> at the specified
2049<code>offset</code>.</p>
2050<p>This function is also available under the <code>readUint32LE</code> alias.</p>
2051<pre><code class="language-js"><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>]);
2052
2053<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>));
2054<span class="hljs-comment">// Prints: 78563412</span>
2055<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>));
2056<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
2057<h4><code>buf.readUIntBE(offset, byteLength)</code><span><a class="mark" href="#buffer_buf_readuintbe_offset_bytelength" id="buffer_buf_readuintbe_offset_bytelength">#</a></span></h4>
2058<div class="api_metadata">
2059<details class="changelog"><summary>History</summary>
2060<table>
2061<tbody><tr><th>Version</th><th>Changes</th></tr>
2062<tr><td>v14.9.0</td>
2063<td><p>This function is also available as <code>buf.readUintBE()</code>.</p></td></tr>
2064<tr><td>v10.0.0</td>
2065<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>
2066<tr><td>v0.11.15</td>
2067<td><p><span>Added in: v0.11.15</span></p></td></tr>
2068</tbody></table>
2069</details>
2070</div>
2071<ul>
2072<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
2073satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
2074<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
2075<code>0 &#x3C; byteLength &#x3C;= 6</code>.</li>
2076<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
2077</ul>
2078<p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code>
2079and interprets the result as an unsigned big-endian integer supporting
2080up to 48 bits of accuracy.</p>
2081<p>This function is also available under the <code>readUintBE</code> alias.</p>
2082<pre><code class="language-js"><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>]);
2083
2084<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>));
2085<span class="hljs-comment">// Prints: 1234567890ab</span>
2086<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>));
2087<span class="hljs-comment">// Throws ERR_OUT_OF_RANGE.</span></code></pre>
2088<h4><code>buf.readUIntLE(offset, byteLength)</code><span><a class="mark" href="#buffer_buf_readuintle_offset_bytelength" id="buffer_buf_readuintle_offset_bytelength">#</a></span></h4>
2089<div class="api_metadata">
2090<details class="changelog"><summary>History</summary>
2091<table>
2092<tbody><tr><th>Version</th><th>Changes</th></tr>
2093<tr><td>v14.9.0</td>
2094<td><p>This function is also available as <code>buf.readUintLE()</code>.</p></td></tr>
2095<tr><td>v10.0.0</td>
2096<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>
2097<tr><td>v0.11.15</td>
2098<td><p><span>Added in: v0.11.15</span></p></td></tr>
2099</tbody></table>
2100</details>
2101</div>
2102<ul>
2103<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
2104satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
2105<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
2106<code>0 &#x3C; byteLength &#x3C;= 6</code>.</li>
2107<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a></li>
2108</ul>
2109<p>Reads <code>byteLength</code> number of bytes from <code>buf</code> at the specified <code>offset</code>
2110and interprets the result as an unsigned, little-endian integer supporting
2111up to 48 bits of accuracy.</p>
2112<p>This function is also available under the <code>readUintLE</code> alias.</p>
2113<pre><code class="language-js"><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>]);
2114
2115<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>));
2116<span class="hljs-comment">// Prints: ab9078563412</span></code></pre>
2117<h4><code>buf.subarray([start[, end]])</code><span><a class="mark" href="#buffer_buf_subarray_start_end" id="buffer_buf_subarray_start_end">#</a></span></h4>
2118<div class="api_metadata">
2119<span>Added in: v3.0.0</span>
2120</div>
2121<ul>
2122<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>
2123<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).
2124<strong>Default:</strong> <a href="#buffer_buf_length"><code>buf.length</code></a>.</li>
2125<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
2126</ul>
2127<p>Returns a new <code>Buffer</code> that references the same memory as the original, but
2128offset and cropped by the <code>start</code> and <code>end</code> indices.</p>
2129<p>Specifying <code>end</code> greater than <a href="#buffer_buf_length"><code>buf.length</code></a> will return the same result as
2130that of <code>end</code> equal to <a href="#buffer_buf_length"><code>buf.length</code></a>.</p>
2131<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>
2132<p>Modifying the new <code>Buffer</code> slice will modify the memory in the original <code>Buffer</code>
2133because the allocated memory of the two objects overlap.</p>
2134<pre><code class="language-js"><span class="hljs-comment">// Create a `Buffer` with the ASCII alphabet, take a slice, and modify one byte</span>
2135<span class="hljs-comment">// from the original `Buffer`.</span>
2136
2137<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>);
2138
2139<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++) {
2140  <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
2141  buf1[i] = i + <span class="hljs-number">97</span>;
2142}
2143
2144<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>);
2145
2146<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>));
2147<span class="hljs-comment">// Prints: abc</span>
2148
2149buf1[<span class="hljs-number">0</span>] = <span class="hljs-number">33</span>;
2150
2151<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>));
2152<span class="hljs-comment">// Prints: !bc</span></code></pre>
2153<p>Specifying negative indexes causes the slice to be generated relative to the
2154end of <code>buf</code> rather than the beginning.</p>
2155<pre><code class="language-js"><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>);
2156
2157<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>());
2158<span class="hljs-comment">// Prints: buffe</span>
2159<span class="hljs-comment">// (Equivalent to buf.subarray(0, 5).)</span>
2160
2161<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>());
2162<span class="hljs-comment">// Prints: buff</span>
2163<span class="hljs-comment">// (Equivalent to buf.subarray(0, 4).)</span>
2164
2165<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>());
2166<span class="hljs-comment">// Prints: uff</span>
2167<span class="hljs-comment">// (Equivalent to buf.subarray(1, 4).)</span></code></pre>
2168<h4><code>buf.slice([start[, end]])</code><span><a class="mark" href="#buffer_buf_slice_start_end" id="buffer_buf_slice_start_end">#</a></span></h4>
2169<div class="api_metadata">
2170<details class="changelog"><summary>History</summary>
2171<table>
2172<tbody><tr><th>Version</th><th>Changes</th></tr>
2173<tr><td>v7.0.0</td>
2174<td><p>All offsets are now coerced to integers before doing any calculations with them.</p></td></tr>
2175<tr><td>v7.1.0, v6.9.2</td>
2176<td><p>Coercing the offsets to integers now handles values outside the 32-bit integer range properly.</p></td></tr>
2177<tr><td>v0.3.0</td>
2178<td><p><span>Added in: v0.3.0</span></p></td></tr>
2179</tbody></table>
2180</details>
2181</div>
2182<ul>
2183<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>
2184<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).
2185<strong>Default:</strong> <a href="#buffer_buf_length"><code>buf.length</code></a>.</li>
2186<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
2187</ul>
2188<p>Returns a new <code>Buffer</code> that references the same memory as the original, but
2189offset and cropped by the <code>start</code> and <code>end</code> indices.</p>
2190<p>This is the same behavior as <code>buf.subarray()</code>.</p>
2191<p>This method is not compatible with the <code>Uint8Array.prototype.slice()</code>,
2192which is a superclass of <code>Buffer</code>. To copy the slice, use
2193<code>Uint8Array.prototype.slice()</code>.</p>
2194<pre><code class="language-js"><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>);
2195
2196<span class="hljs-keyword">const</span> copiedBuf = <span class="hljs-title class_">Uint</span>8<span class="hljs-built_in">Array</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);
2197copiedBuf[<span class="hljs-number">0</span>]++;
2198<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(copiedBuf.<span class="hljs-title function_">toString</span>());
2199<span class="hljs-comment">// Prints: cuffer</span>
2200
2201<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf.<span class="hljs-title function_">toString</span>());
2202<span class="hljs-comment">// Prints: buffer</span></code></pre>
2203<h4><code>buf.swap16()</code><span><a class="mark" href="#buffer_buf_swap16" id="buffer_buf_swap16">#</a></span></h4>
2204<div class="api_metadata">
2205<span>Added in: v5.10.0</span>
2206</div>
2207<ul>
2208<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> A reference to <code>buf</code>.</li>
2209</ul>
2210<p>Interprets <code>buf</code> as an array of unsigned 16-bit integers and swaps the
2211byte order <em>in-place</em>. Throws <a href="errors.html#ERR_INVALID_BUFFER_SIZE"><code>ERR_INVALID_BUFFER_SIZE</code></a> if <a href="#buffer_buf_length"><code>buf.length</code></a>
2212is not a multiple of 2.</p>
2213<pre><code class="language-js"><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>]);
2214
2215<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
2216<span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span>
2217
2218buf1.<span class="hljs-title function_">swap16</span>();
2219
2220<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
2221<span class="hljs-comment">// Prints: &#x3C;Buffer 02 01 04 03 06 05 08 07></span>
2222
2223<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>]);
2224
2225buf2.<span class="hljs-title function_">swap16</span>();
2226<span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code></pre>
2227<p>One convenient use of <code>buf.swap16()</code> is to perform a fast in-place conversion
2228between UTF-16 little-endian and UTF-16 big-endian:</p>
2229<pre><code class="language-js"><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>);
2230buf.<span class="hljs-title function_">swap16</span>(); <span class="hljs-comment">// Convert to big-endian UTF-16 text.</span></code></pre>
2231<h4><code>buf.swap32()</code><span><a class="mark" href="#buffer_buf_swap32" id="buffer_buf_swap32">#</a></span></h4>
2232<div class="api_metadata">
2233<span>Added in: v5.10.0</span>
2234</div>
2235<ul>
2236<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> A reference to <code>buf</code>.</li>
2237</ul>
2238<p>Interprets <code>buf</code> as an array of unsigned 32-bit integers and swaps the
2239byte order <em>in-place</em>. Throws <a href="errors.html#ERR_INVALID_BUFFER_SIZE"><code>ERR_INVALID_BUFFER_SIZE</code></a> if <a href="#buffer_buf_length"><code>buf.length</code></a>
2240is not a multiple of 4.</p>
2241<pre><code class="language-js"><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>]);
2242
2243<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
2244<span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span>
2245
2246buf1.<span class="hljs-title function_">swap32</span>();
2247
2248<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
2249<span class="hljs-comment">// Prints: &#x3C;Buffer 04 03 02 01 08 07 06 05></span>
2250
2251<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>]);
2252
2253buf2.<span class="hljs-title function_">swap32</span>();
2254<span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code></pre>
2255<h4><code>buf.swap64()</code><span><a class="mark" href="#buffer_buf_swap64" id="buffer_buf_swap64">#</a></span></h4>
2256<div class="api_metadata">
2257<span>Added in: v6.3.0</span>
2258</div>
2259<ul>
2260<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a> A reference to <code>buf</code>.</li>
2261</ul>
2262<p>Interprets <code>buf</code> as an array of 64-bit numbers and swaps byte order <em>in-place</em>.
2263Throws <a href="errors.html#ERR_INVALID_BUFFER_SIZE"><code>ERR_INVALID_BUFFER_SIZE</code></a> if <a href="#buffer_buf_length"><code>buf.length</code></a> is not a multiple of 8.</p>
2264<pre><code class="language-js"><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>]);
2265
2266<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
2267<span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span>
2268
2269buf1.<span class="hljs-title function_">swap64</span>();
2270
2271<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf1);
2272<span class="hljs-comment">// Prints: &#x3C;Buffer 08 07 06 05 04 03 02 01></span>
2273
2274<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>]);
2275
2276buf2.<span class="hljs-title function_">swap64</span>();
2277<span class="hljs-comment">// Throws ERR_INVALID_BUFFER_SIZE.</span></code></pre>
2278<h4><code>buf.toJSON()</code><span><a class="mark" href="#buffer_buf_tojson" id="buffer_buf_tojson">#</a></span></h4>
2279<div class="api_metadata">
2280<span>Added in: v0.9.2</span>
2281</div>
2282<ul>
2283<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
2284</ul>
2285<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
2286this function when stringifying a <code>Buffer</code> instance.</p>
2287<p><code>Buffer.from()</code> accepts objects in the format returned from this method.
2288In particular, <code>Buffer.from(buf.toJSON())</code> works like <code>Buffer.from(buf)</code>.</p>
2289<pre><code class="language-js"><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>]);
2290<span class="hljs-keyword">const</span> json = <span class="hljs-variable constant_">JSON</span>.<span class="hljs-title function_">stringify</span>(buf);
2291
2292<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(json);
2293<span class="hljs-comment">// Prints: {"type":"Buffer","data":[1,2,3,4,5]}</span>
2294
2295<span class="hljs-keyword">const</span> copy = <span class="hljs-variable constant_">JSON</span>.<span class="hljs-title function_">parse</span>(json, <span class="hljs-function">(<span class="hljs-params">key, value</span>) =></span> {
2296  <span class="hljs-keyword">return</span> value &#x26;&#x26; value.<span class="hljs-property">type</span> === <span class="hljs-string">'Buffer'</span> ?
2297    <span class="hljs-title class_">Buffer</span>.<span class="hljs-title function_">from</span>(value) :
2298    value;
2299});
2300
2301<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(copy);
2302<span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05></span></code></pre>
2303<h4><code>buf.toString([encoding[, start[, end]]])</code><span><a class="mark" href="#buffer_buf_tostring_encoding_start_end" id="buffer_buf_tostring_encoding_start_end">#</a></span></h4>
2304<div class="api_metadata">
2305<span>Added in: v0.1.90</span>
2306</div>
2307<ul>
2308<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>
2309<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>
2310<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).
2311<strong>Default:</strong> <a href="#buffer_buf_length"><code>buf.length</code></a>.</li>
2312<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
2313</ul>
2314<p>Decodes <code>buf</code> to a string according to the specified character encoding in
2315<code>encoding</code>. <code>start</code> and <code>end</code> may be passed to decode only a subset of <code>buf</code>.</p>
2316<p>If <code>encoding</code> is <code>'utf8'</code> and a byte sequence in the input is not valid UTF-8,
2317then each invalid byte is replaced with the replacement character <code>U+FFFD</code>.</p>
2318<p>The maximum length of a string instance (in UTF-16 code units) is available
2319as <a href="#buffer_buffer_constants_max_string_length"><code>buffer.constants.MAX_STRING_LENGTH</code></a>.</p>
2320<pre><code class="language-js"><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>);
2321
2322<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++) {
2323  <span class="hljs-comment">// 97 is the decimal ASCII value for 'a'.</span>
2324  buf1[i] = i + <span class="hljs-number">97</span>;
2325}
2326
2327<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>));
2328<span class="hljs-comment">// Prints: abcdefghijklmnopqrstuvwxyz</span>
2329<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>));
2330<span class="hljs-comment">// Prints: abcde</span>
2331
2332<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>);
2333
2334<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>));
2335<span class="hljs-comment">// Prints: 74c3a97374</span>
2336<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>));
2337<span class="hljs-comment">// Prints: té</span>
2338<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>));
2339<span class="hljs-comment">// Prints: té</span></code></pre>
2340<h4><code>buf.values()</code><span><a class="mark" href="#buffer_buf_values" id="buffer_buf_values">#</a></span></h4>
2341<div class="api_metadata">
2342<span>Added in: v1.1.0</span>
2343</div>
2344<ul>
2345<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>
2346</ul>
2347<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
2348called automatically when a <code>Buffer</code> is used in a <code>for..of</code> statement.</p>
2349<pre><code class="language-js"><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>);
2350
2351<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>()) {
2352  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value);
2353}
2354<span class="hljs-comment">// Prints:</span>
2355<span class="hljs-comment">//   98</span>
2356<span class="hljs-comment">//   117</span>
2357<span class="hljs-comment">//   102</span>
2358<span class="hljs-comment">//   102</span>
2359<span class="hljs-comment">//   101</span>
2360<span class="hljs-comment">//   114</span>
2361
2362<span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> value <span class="hljs-keyword">of</span> buf) {
2363  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(value);
2364}
2365<span class="hljs-comment">// Prints:</span>
2366<span class="hljs-comment">//   98</span>
2367<span class="hljs-comment">//   117</span>
2368<span class="hljs-comment">//   102</span>
2369<span class="hljs-comment">//   102</span>
2370<span class="hljs-comment">//   101</span>
2371<span class="hljs-comment">//   114</span></code></pre>
2372<h4><code>buf.write(string[, offset[, length]][, encoding])</code><span><a class="mark" href="#buffer_buf_write_string_offset_length_encoding" id="buffer_buf_write_string_offset_length_encoding">#</a></span></h4>
2373<div class="api_metadata">
2374<span>Added in: v0.1.90</span>
2375</div>
2376<ul>
2377<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>
2378<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>.
2379<strong>Default:</strong> <code>0</code>.</li>
2380<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
2381exceed <code>buf.length - offset</code>). <strong>Default:</strong> <code>buf.length - offset</code>.</li>
2382<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>
2383<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>
2384</ul>
2385<p>Writes <code>string</code> to <code>buf</code> at <code>offset</code> according to the character encoding in
2386<code>encoding</code>. The <code>length</code> parameter is the number of bytes to write. If <code>buf</code> did
2387not contain enough space to fit the entire string, only part of <code>string</code> will be
2388written. However, partially encoded characters will not be written.</p>
2389<pre><code class="language-js"><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>);
2390
2391<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>);
2392
2393<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>);
2394<span class="hljs-comment">// Prints: 12 bytes: ½ + ¼ = ¾</span>
2395
2396<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>);
2397
2398<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>);
2399
2400<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>);
2401<span class="hljs-comment">// Prints: 2 bytes : ab</span></code></pre>
2402<h4><code>buf.writeBigInt64BE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writebigint64be_value_offset" id="buffer_buf_writebigint64be_value_offset">#</a></span></h4>
2403<div class="api_metadata">
2404<span>Added in: v12.0.0, v10.20.0</span>
2405</div>
2406<ul>
2407<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>
2408<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
2409satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
2410<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>
2411</ul>
2412<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian.</p>
2413<p><code>value</code> is interpreted and written as a two's complement signed integer.</p>
2414<pre><code class="language-js"><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>);
2415
2416buf.<span class="hljs-title function_">writeBigInt64BE</span>(<span class="hljs-number">0x0102030405060708n</span>, <span class="hljs-number">0</span>);
2417
2418<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
2419<span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04 05 06 07 08></span></code></pre>
2420<h4><code>buf.writeBigInt64LE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writebigint64le_value_offset" id="buffer_buf_writebigint64le_value_offset">#</a></span></h4>
2421<div class="api_metadata">
2422<span>Added in: v12.0.0, v10.20.0</span>
2423</div>
2424<ul>
2425<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>
2426<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
2427satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
2428<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>
2429</ul>
2430<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian.</p>
2431<p><code>value</code> is interpreted and written as a two's complement signed integer.</p>
2432<pre><code class="language-js"><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>);
2433
2434buf.<span class="hljs-title function_">writeBigInt64LE</span>(<span class="hljs-number">0x0102030405060708n</span>, <span class="hljs-number">0</span>);
2435
2436<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
2437<span class="hljs-comment">// Prints: &#x3C;Buffer 08 07 06 05 04 03 02 01></span></code></pre>
2438<h4><code>buf.writeBigUInt64BE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writebiguint64be_value_offset" id="buffer_buf_writebiguint64be_value_offset">#</a></span></h4>
2439<div class="api_metadata">
2440<details class="changelog"><summary>History</summary>
2441<table>
2442<tbody><tr><th>Version</th><th>Changes</th></tr>
2443<tr><td>v14.10.0</td>
2444<td><p>This function is also available as <code>buf.writeBigUint64BE()</code>.</p></td></tr>
2445<tr><td>v12.0.0, v10.20.0</td>
2446<td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr>
2447</tbody></table>
2448</details>
2449</div>
2450<ul>
2451<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>
2452<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
2453satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
2454<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>
2455</ul>
2456<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian.</p>
2457<p>This function is also available under the <code>writeBigUint64BE</code> alias.</p>
2458<pre><code class="language-js"><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>);
2459
2460buf.<span class="hljs-title function_">writeBigUInt64BE</span>(<span class="hljs-number">0xdecafafecacefaden</span>, <span class="hljs-number">0</span>);
2461
2462<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
2463<span class="hljs-comment">// Prints: &#x3C;Buffer de ca fa fe ca ce fa de></span></code></pre>
2464<h4><code>buf.writeBigUInt64LE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writebiguint64le_value_offset" id="buffer_buf_writebiguint64le_value_offset">#</a></span></h4>
2465<div class="api_metadata">
2466<details class="changelog"><summary>History</summary>
2467<table>
2468<tbody><tr><th>Version</th><th>Changes</th></tr>
2469<tr><td>v14.10.0</td>
2470<td><p>This function is also available as <code>buf.writeBigUint64LE()</code>.</p></td></tr>
2471<tr><td>v12.0.0, v10.20.0</td>
2472<td><p><span>Added in: v12.0.0, v10.20.0</span></p></td></tr>
2473</tbody></table>
2474</details>
2475</div>
2476<ul>
2477<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>
2478<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
2479satisfy: <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
2480<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>
2481</ul>
2482<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian</p>
2483<pre><code class="language-js"><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>);
2484
2485buf.<span class="hljs-title function_">writeBigUInt64LE</span>(<span class="hljs-number">0xdecafafecacefaden</span>, <span class="hljs-number">0</span>);
2486
2487<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
2488<span class="hljs-comment">// Prints: &#x3C;Buffer de fa ce ca fe fa ca de></span></code></pre>
2489<p>This function is also available under the <code>writeBigUint64LE</code> alias.</p>
2490<h4><code>buf.writeDoubleBE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writedoublebe_value_offset" id="buffer_buf_writedoublebe_value_offset">#</a></span></h4>
2491<div class="api_metadata">
2492<details class="changelog"><summary>History</summary>
2493<table>
2494<tbody><tr><th>Version</th><th>Changes</th></tr>
2495<tr><td>v10.0.0</td>
2496<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2497<tr><td>v0.11.15</td>
2498<td><p><span>Added in: v0.11.15</span></p></td></tr>
2499</tbody></table>
2500</details>
2501</div>
2502<ul>
2503<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>
2504<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
2505satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
2506<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>
2507</ul>
2508<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code>
2509must be a JavaScript number. Behavior is undefined when <code>value</code> is anything
2510other than a JavaScript number.</p>
2511<pre><code class="language-js"><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>);
2512
2513buf.<span class="hljs-title function_">writeDoubleBE</span>(<span class="hljs-number">123.456</span>, <span class="hljs-number">0</span>);
2514
2515<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
2516<span class="hljs-comment">// Prints: &#x3C;Buffer 40 5e dd 2f 1a 9f be 77></span></code></pre>
2517<h4><code>buf.writeDoubleLE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writedoublele_value_offset" id="buffer_buf_writedoublele_value_offset">#</a></span></h4>
2518<div class="api_metadata">
2519<details class="changelog"><summary>History</summary>
2520<table>
2521<tbody><tr><th>Version</th><th>Changes</th></tr>
2522<tr><td>v10.0.0</td>
2523<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2524<tr><td>v0.11.15</td>
2525<td><p><span>Added in: v0.11.15</span></p></td></tr>
2526</tbody></table>
2527</details>
2528</div>
2529<ul>
2530<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>
2531<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
2532satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 8</code>. <strong>Default:</strong> <code>0</code>.</li>
2533<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>
2534</ul>
2535<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code>
2536must be a JavaScript number. Behavior is undefined when <code>value</code> is anything
2537other than a JavaScript number.</p>
2538<pre><code class="language-js"><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>);
2539
2540buf.<span class="hljs-title function_">writeDoubleLE</span>(<span class="hljs-number">123.456</span>, <span class="hljs-number">0</span>);
2541
2542<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
2543<span class="hljs-comment">// Prints: &#x3C;Buffer 77 be 9f 1a 2f dd 5e 40></span></code></pre>
2544<h4><code>buf.writeFloatBE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writefloatbe_value_offset" id="buffer_buf_writefloatbe_value_offset">#</a></span></h4>
2545<div class="api_metadata">
2546<details class="changelog"><summary>History</summary>
2547<table>
2548<tbody><tr><th>Version</th><th>Changes</th></tr>
2549<tr><td>v10.0.0</td>
2550<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2551<tr><td>v0.11.15</td>
2552<td><p><span>Added in: v0.11.15</span></p></td></tr>
2553</tbody></table>
2554</details>
2555</div>
2556<ul>
2557<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>
2558<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
2559satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
2560<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>
2561</ul>
2562<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. Behavior is
2563undefined when <code>value</code> is anything other than a JavaScript number.</p>
2564<pre><code class="language-js"><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>);
2565
2566buf.<span class="hljs-title function_">writeFloatBE</span>(<span class="hljs-number">0xcafebabe</span>, <span class="hljs-number">0</span>);
2567
2568<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
2569<span class="hljs-comment">// Prints: &#x3C;Buffer 4f 4a fe bb></span></code></pre>
2570<h4><code>buf.writeFloatLE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writefloatle_value_offset" id="buffer_buf_writefloatle_value_offset">#</a></span></h4>
2571<div class="api_metadata">
2572<details class="changelog"><summary>History</summary>
2573<table>
2574<tbody><tr><th>Version</th><th>Changes</th></tr>
2575<tr><td>v10.0.0</td>
2576<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2577<tr><td>v0.11.15</td>
2578<td><p><span>Added in: v0.11.15</span></p></td></tr>
2579</tbody></table>
2580</details>
2581</div>
2582<ul>
2583<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>
2584<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
2585satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
2586<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>
2587</ul>
2588<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. Behavior is
2589undefined when <code>value</code> is anything other than a JavaScript number.</p>
2590<pre><code class="language-js"><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>);
2591
2592buf.<span class="hljs-title function_">writeFloatLE</span>(<span class="hljs-number">0xcafebabe</span>, <span class="hljs-number">0</span>);
2593
2594<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
2595<span class="hljs-comment">// Prints: &#x3C;Buffer bb fe 4a 4f></span></code></pre>
2596<h4><code>buf.writeInt8(value[, offset])</code><span><a class="mark" href="#buffer_buf_writeint8_value_offset" id="buffer_buf_writeint8_value_offset">#</a></span></h4>
2597<div class="api_metadata">
2598<details class="changelog"><summary>History</summary>
2599<table>
2600<tbody><tr><th>Version</th><th>Changes</th></tr>
2601<tr><td>v10.0.0</td>
2602<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2603<tr><td>v0.5.0</td>
2604<td><p><span>Added in: v0.5.0</span></p></td></tr>
2605</tbody></table>
2606</details>
2607</div>
2608<ul>
2609<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>
2610<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
2611satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li>
2612<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>
2613</ul>
2614<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code>. <code>value</code> must be a valid
2615signed 8-bit integer. Behavior is undefined when <code>value</code> is anything other than
2616a signed 8-bit integer.</p>
2617<p><code>value</code> is interpreted and written as a two's complement signed integer.</p>
2618<pre><code class="language-js"><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>);
2619
2620buf.<span class="hljs-title function_">writeInt8</span>(<span class="hljs-number">2</span>, <span class="hljs-number">0</span>);
2621buf.<span class="hljs-title function_">writeInt8</span>(-<span class="hljs-number">2</span>, <span class="hljs-number">1</span>);
2622
2623<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
2624<span class="hljs-comment">// Prints: &#x3C;Buffer 02 fe></span></code></pre>
2625<h4><code>buf.writeInt16BE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writeint16be_value_offset" id="buffer_buf_writeint16be_value_offset">#</a></span></h4>
2626<div class="api_metadata">
2627<details class="changelog"><summary>History</summary>
2628<table>
2629<tbody><tr><th>Version</th><th>Changes</th></tr>
2630<tr><td>v10.0.0</td>
2631<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2632<tr><td>v0.5.5</td>
2633<td><p><span>Added in: v0.5.5</span></p></td></tr>
2634</tbody></table>
2635</details>
2636</div>
2637<ul>
2638<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>
2639<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
2640satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li>
2641<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>
2642</ul>
2643<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian.  The <code>value</code>
2644must be a valid signed 16-bit integer. Behavior is undefined when <code>value</code> is
2645anything other than a signed 16-bit integer.</p>
2646<p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p>
2647<pre><code class="language-js"><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>);
2648
2649buf.<span class="hljs-title function_">writeInt16BE</span>(<span class="hljs-number">0x0102</span>, <span class="hljs-number">0</span>);
2650
2651<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
2652<span class="hljs-comment">// Prints: &#x3C;Buffer 01 02></span></code></pre>
2653<h4><code>buf.writeInt16LE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writeint16le_value_offset" id="buffer_buf_writeint16le_value_offset">#</a></span></h4>
2654<div class="api_metadata">
2655<details class="changelog"><summary>History</summary>
2656<table>
2657<tbody><tr><th>Version</th><th>Changes</th></tr>
2658<tr><td>v10.0.0</td>
2659<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2660<tr><td>v0.5.5</td>
2661<td><p><span>Added in: v0.5.5</span></p></td></tr>
2662</tbody></table>
2663</details>
2664</div>
2665<ul>
2666<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>
2667<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
2668satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li>
2669<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>
2670</ul>
2671<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian.  The <code>value</code>
2672must be a valid signed 16-bit integer. Behavior is undefined when <code>value</code> is
2673anything other than a signed 16-bit integer.</p>
2674<p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p>
2675<pre><code class="language-js"><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>);
2676
2677buf.<span class="hljs-title function_">writeInt16LE</span>(<span class="hljs-number">0x0304</span>, <span class="hljs-number">0</span>);
2678
2679<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
2680<span class="hljs-comment">// Prints: &#x3C;Buffer 04 03></span></code></pre>
2681<h4><code>buf.writeInt32BE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writeint32be_value_offset" id="buffer_buf_writeint32be_value_offset">#</a></span></h4>
2682<div class="api_metadata">
2683<details class="changelog"><summary>History</summary>
2684<table>
2685<tbody><tr><th>Version</th><th>Changes</th></tr>
2686<tr><td>v10.0.0</td>
2687<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2688<tr><td>v0.5.5</td>
2689<td><p><span>Added in: v0.5.5</span></p></td></tr>
2690</tbody></table>
2691</details>
2692</div>
2693<ul>
2694<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>
2695<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
2696satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
2697<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>
2698</ul>
2699<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code>
2700must be a valid signed 32-bit integer. Behavior is undefined when <code>value</code> is
2701anything other than a signed 32-bit integer.</p>
2702<p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p>
2703<pre><code class="language-js"><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>);
2704
2705buf.<span class="hljs-title function_">writeInt32BE</span>(<span class="hljs-number">0x01020304</span>, <span class="hljs-number">0</span>);
2706
2707<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
2708<span class="hljs-comment">// Prints: &#x3C;Buffer 01 02 03 04></span></code></pre>
2709<h4><code>buf.writeInt32LE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writeint32le_value_offset" id="buffer_buf_writeint32le_value_offset">#</a></span></h4>
2710<div class="api_metadata">
2711<details class="changelog"><summary>History</summary>
2712<table>
2713<tbody><tr><th>Version</th><th>Changes</th></tr>
2714<tr><td>v10.0.0</td>
2715<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2716<tr><td>v0.5.5</td>
2717<td><p><span>Added in: v0.5.5</span></p></td></tr>
2718</tbody></table>
2719</details>
2720</div>
2721<ul>
2722<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>
2723<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
2724satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
2725<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>
2726</ul>
2727<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code>
2728must be a valid signed 32-bit integer. Behavior is undefined when <code>value</code> is
2729anything other than a signed 32-bit integer.</p>
2730<p>The <code>value</code> is interpreted and written as a two's complement signed integer.</p>
2731<pre><code class="language-js"><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>);
2732
2733buf.<span class="hljs-title function_">writeInt32LE</span>(<span class="hljs-number">0x05060708</span>, <span class="hljs-number">0</span>);
2734
2735<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
2736<span class="hljs-comment">// Prints: &#x3C;Buffer 08 07 06 05></span></code></pre>
2737<h4><code>buf.writeIntBE(value, offset, byteLength)</code><span><a class="mark" href="#buffer_buf_writeintbe_value_offset_bytelength" id="buffer_buf_writeintbe_value_offset_bytelength">#</a></span></h4>
2738<div class="api_metadata">
2739<details class="changelog"><summary>History</summary>
2740<table>
2741<tbody><tr><th>Version</th><th>Changes</th></tr>
2742<tr><td>v10.0.0</td>
2743<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>
2744<tr><td>v0.11.15</td>
2745<td><p><span>Added in: v0.11.15</span></p></td></tr>
2746</tbody></table>
2747</details>
2748</div>
2749<ul>
2750<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>
2751<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
2752satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
2753<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
2754<code>0 &#x3C; byteLength &#x3C;= 6</code>.</li>
2755<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>
2756</ul>
2757<p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code>
2758as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined when
2759<code>value</code> is anything other than a signed integer.</p>
2760<pre><code class="language-js"><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>);
2761
2762buf.<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>);
2763
2764<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
2765<span class="hljs-comment">// Prints: &#x3C;Buffer 12 34 56 78 90 ab></span></code></pre>
2766<h4><code>buf.writeIntLE(value, offset, byteLength)</code><span><a class="mark" href="#buffer_buf_writeintle_value_offset_bytelength" id="buffer_buf_writeintle_value_offset_bytelength">#</a></span></h4>
2767<div class="api_metadata">
2768<details class="changelog"><summary>History</summary>
2769<table>
2770<tbody><tr><th>Version</th><th>Changes</th></tr>
2771<tr><td>v10.0.0</td>
2772<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>
2773<tr><td>v0.11.15</td>
2774<td><p><span>Added in: v0.11.15</span></p></td></tr>
2775</tbody></table>
2776</details>
2777</div>
2778<ul>
2779<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>
2780<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
2781satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
2782<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
2783<code>0 &#x3C; byteLength &#x3C;= 6</code>.</li>
2784<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>
2785</ul>
2786<p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code>
2787as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined
2788when <code>value</code> is anything other than a signed integer.</p>
2789<pre><code class="language-js"><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>);
2790
2791buf.<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>);
2792
2793<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
2794<span class="hljs-comment">// Prints: &#x3C;Buffer ab 90 78 56 34 12></span></code></pre>
2795<h4><code>buf.writeUInt8(value[, offset])</code><span><a class="mark" href="#buffer_buf_writeuint8_value_offset" id="buffer_buf_writeuint8_value_offset">#</a></span></h4>
2796<div class="api_metadata">
2797<details class="changelog"><summary>History</summary>
2798<table>
2799<tbody><tr><th>Version</th><th>Changes</th></tr>
2800<tr><td>v14.9.0</td>
2801<td><p>This function is also available as <code>buf.writeUint8()</code>.</p></td></tr>
2802<tr><td>v10.0.0</td>
2803<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2804<tr><td>v0.5.0</td>
2805<td><p><span>Added in: v0.5.0</span></p></td></tr>
2806</tbody></table>
2807</details>
2808</div>
2809<ul>
2810<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>
2811<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
2812satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 1</code>. <strong>Default:</strong> <code>0</code>.</li>
2813<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>
2814</ul>
2815<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code>. <code>value</code> must be a
2816valid unsigned 8-bit integer. Behavior is undefined when <code>value</code> is anything
2817other than an unsigned 8-bit integer.</p>
2818<p>This function is also available under the <code>writeUint8</code> alias.</p>
2819<pre><code class="language-js"><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>);
2820
2821buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x3</span>, <span class="hljs-number">0</span>);
2822buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x4</span>, <span class="hljs-number">1</span>);
2823buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x23</span>, <span class="hljs-number">2</span>);
2824buf.<span class="hljs-title function_">writeUInt8</span>(<span class="hljs-number">0x42</span>, <span class="hljs-number">3</span>);
2825
2826<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
2827<span class="hljs-comment">// Prints: &#x3C;Buffer 03 04 23 42></span></code></pre>
2828<h4><code>buf.writeUInt16BE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writeuint16be_value_offset" id="buffer_buf_writeuint16be_value_offset">#</a></span></h4>
2829<div class="api_metadata">
2830<details class="changelog"><summary>History</summary>
2831<table>
2832<tbody><tr><th>Version</th><th>Changes</th></tr>
2833<tr><td>v14.9.0</td>
2834<td><p>This function is also available as <code>buf.writeUint16BE()</code>.</p></td></tr>
2835<tr><td>v10.0.0</td>
2836<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2837<tr><td>v0.5.5</td>
2838<td><p><span>Added in: v0.5.5</span></p></td></tr>
2839</tbody></table>
2840</details>
2841</div>
2842<ul>
2843<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>
2844<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
2845satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li>
2846<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>
2847</ul>
2848<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code>
2849must be a valid unsigned 16-bit integer. Behavior is undefined when <code>value</code>
2850is anything other than an unsigned 16-bit integer.</p>
2851<p>This function is also available under the <code>writeUint16BE</code> alias.</p>
2852<pre><code class="language-js"><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>);
2853
2854buf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xdead</span>, <span class="hljs-number">0</span>);
2855buf.<span class="hljs-title function_">writeUInt16BE</span>(<span class="hljs-number">0xbeef</span>, <span class="hljs-number">2</span>);
2856
2857<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
2858<span class="hljs-comment">// Prints: &#x3C;Buffer de ad be ef></span></code></pre>
2859<h4><code>buf.writeUInt16LE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writeuint16le_value_offset" id="buffer_buf_writeuint16le_value_offset">#</a></span></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>v14.9.0, v12.19.0</td>
2865<td><p>This function is also available as <code>buf.writeUint16LE()</code>.</p></td></tr>
2866<tr><td>v10.0.0</td>
2867<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2868<tr><td>v0.5.5</td>
2869<td><p><span>Added in: v0.5.5</span></p></td></tr>
2870</tbody></table>
2871</details>
2872</div>
2873<ul>
2874<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>
2875<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
2876satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 2</code>. <strong>Default:</strong> <code>0</code>.</li>
2877<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>
2878</ul>
2879<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code>
2880must be a valid unsigned 16-bit integer. Behavior is undefined when <code>value</code> is
2881anything other than an unsigned 16-bit integer.</p>
2882<p>This function is also available under the <code>writeUint16LE</code> alias.</p>
2883<pre><code class="language-js"><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>);
2884
2885buf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xdead</span>, <span class="hljs-number">0</span>);
2886buf.<span class="hljs-title function_">writeUInt16LE</span>(<span class="hljs-number">0xbeef</span>, <span class="hljs-number">2</span>);
2887
2888<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
2889<span class="hljs-comment">// Prints: &#x3C;Buffer ad de ef be></span></code></pre>
2890<h4><code>buf.writeUInt32BE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writeuint32be_value_offset" id="buffer_buf_writeuint32be_value_offset">#</a></span></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>v14.9.0</td>
2896<td><p>This function is also available as <code>buf.writeUint32BE()</code>.</p></td></tr>
2897<tr><td>v10.0.0</td>
2898<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2899<tr><td>v0.5.5</td>
2900<td><p><span>Added in: v0.5.5</span></p></td></tr>
2901</tbody></table>
2902</details>
2903</div>
2904<ul>
2905<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>
2906<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
2907satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
2908<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>
2909</ul>
2910<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as big-endian. The <code>value</code>
2911must be a valid unsigned 32-bit integer. Behavior is undefined when <code>value</code>
2912is anything other than an unsigned 32-bit integer.</p>
2913<p>This function is also available under the <code>writeUint32BE</code> alias.</p>
2914<pre><code class="language-js"><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>);
2915
2916buf.<span class="hljs-title function_">writeUInt32BE</span>(<span class="hljs-number">0xfeedface</span>, <span class="hljs-number">0</span>);
2917
2918<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
2919<span class="hljs-comment">// Prints: &#x3C;Buffer fe ed fa ce></span></code></pre>
2920<h4><code>buf.writeUInt32LE(value[, offset])</code><span><a class="mark" href="#buffer_buf_writeuint32le_value_offset" id="buffer_buf_writeuint32le_value_offset">#</a></span></h4>
2921<div class="api_metadata">
2922<details class="changelog"><summary>History</summary>
2923<table>
2924<tbody><tr><th>Version</th><th>Changes</th></tr>
2925<tr><td>v14.9.0</td>
2926<td><p>This function is also available as <code>buf.writeUint32LE()</code>.</p></td></tr>
2927<tr><td>v10.0.0</td>
2928<td><p>Removed <code>noAssert</code> and no implicit coercion of the offset to <code>uint32</code> anymore.</p></td></tr>
2929<tr><td>v0.5.5</td>
2930<td><p><span>Added in: v0.5.5</span></p></td></tr>
2931</tbody></table>
2932</details>
2933</div>
2934<ul>
2935<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>
2936<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
2937satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - 4</code>. <strong>Default:</strong> <code>0</code>.</li>
2938<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>
2939</ul>
2940<p>Writes <code>value</code> to <code>buf</code> at the specified <code>offset</code> as little-endian. The <code>value</code>
2941must be a valid unsigned 32-bit integer. Behavior is undefined when <code>value</code> is
2942anything other than an unsigned 32-bit integer.</p>
2943<p>This function is also available under the <code>writeUint32LE</code> alias.</p>
2944<pre><code class="language-js"><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>);
2945
2946buf.<span class="hljs-title function_">writeUInt32LE</span>(<span class="hljs-number">0xfeedface</span>, <span class="hljs-number">0</span>);
2947
2948<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
2949<span class="hljs-comment">// Prints: &#x3C;Buffer ce fa ed fe></span></code></pre>
2950<h4><code>buf.writeUIntBE(value, offset, byteLength)</code><span><a class="mark" href="#buffer_buf_writeuintbe_value_offset_bytelength" id="buffer_buf_writeuintbe_value_offset_bytelength">#</a></span></h4>
2951<div class="api_metadata">
2952<details class="changelog"><summary>History</summary>
2953<table>
2954<tbody><tr><th>Version</th><th>Changes</th></tr>
2955<tr><td>v14.9.0</td>
2956<td><p>This function is also available as <code>buf.writeUintBE()</code>.</p></td></tr>
2957<tr><td>v10.0.0</td>
2958<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>
2959<tr><td>v0.5.5</td>
2960<td><p><span>Added in: v0.5.5</span></p></td></tr>
2961</tbody></table>
2962</details>
2963</div>
2964<ul>
2965<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>
2966<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
2967satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
2968<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
2969<code>0 &#x3C; byteLength &#x3C;= 6</code>.</li>
2970<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>
2971</ul>
2972<p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code>
2973as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined
2974when <code>value</code> is anything other than an unsigned integer.</p>
2975<p>This function is also available under the <code>writeUintBE</code> alias.</p>
2976<pre><code class="language-js"><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>);
2977
2978buf.<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>);
2979
2980<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
2981<span class="hljs-comment">// Prints: &#x3C;Buffer 12 34 56 78 90 ab></span></code></pre>
2982<h4><code>buf.writeUIntLE(value, offset, byteLength)</code><span><a class="mark" href="#buffer_buf_writeuintle_value_offset_bytelength" id="buffer_buf_writeuintle_value_offset_bytelength">#</a></span></h4>
2983<div class="api_metadata">
2984<details class="changelog"><summary>History</summary>
2985<table>
2986<tbody><tr><th>Version</th><th>Changes</th></tr>
2987<tr><td>v14.9.0</td>
2988<td><p>This function is also available as <code>buf.writeUintLE()</code>.</p></td></tr>
2989<tr><td>v10.0.0</td>
2990<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>
2991<tr><td>v0.5.5</td>
2992<td><p><span>Added in: v0.5.5</span></p></td></tr>
2993</tbody></table>
2994</details>
2995</div>
2996<ul>
2997<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>
2998<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
2999satisfy <code>0 &#x3C;= offset &#x3C;= buf.length - byteLength</code>.</li>
3000<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
3001<code>0 &#x3C; byteLength &#x3C;= 6</code>.</li>
3002<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>
3003</ul>
3004<p>Writes <code>byteLength</code> bytes of <code>value</code> to <code>buf</code> at the specified <code>offset</code>
3005as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined
3006when <code>value</code> is anything other than an unsigned integer.</p>
3007<p>This function is also available under the <code>writeUintLE</code> alias.</p>
3008<pre><code class="language-js"><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>);
3009
3010buf.<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>);
3011
3012<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(buf);
3013<span class="hljs-comment">// Prints: &#x3C;Buffer ab 90 78 56 34 12></span></code></pre>
3014<h4><code>new Buffer(array)</code><span><a class="mark" href="#buffer_new_buffer_array" id="buffer_new_buffer_array">#</a></span></h4>
3015<div class="api_metadata">
3016<details class="changelog"><summary>History</summary>
3017<table>
3018<tbody><tr><th>Version</th><th>Changes</th></tr>
3019<tr><td>v10.0.0</td>
3020<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr>
3021<tr><td>v7.2.1</td>
3022<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr>
3023<tr><td>v7.0.0</td>
3024<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr>
3025<tr><td>v6.0.0</td>
3026<td><p><span>Deprecated since: v6.0.0</span></p></td></tr>
3027</tbody></table>
3028</details>
3029</div>
3030<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#buffer_static_method_buffer_from_array"><code>Buffer.from(array)</code></a> instead.</div><p></p>
3031<ul>
3032<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>
3033</ul>
3034<p>See <a href="#buffer_static_method_buffer_from_array"><code>Buffer.from(array)</code></a>.</p>
3035<h4><code>new Buffer(arrayBuffer[, byteOffset[, length]])</code><span><a class="mark" href="#buffer_new_buffer_arraybuffer_byteoffset_length" id="buffer_new_buffer_arraybuffer_byteoffset_length">#</a></span></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>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr>
3042<tr><td>v7.2.1</td>
3043<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr>
3044<tr><td>v7.0.0</td>
3045<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr>
3046<tr><td>v6.0.0</td>
3047<td><p>The <code>byteOffset</code> and <code>length</code> parameters are supported now.</p></td></tr>
3048<tr><td>v6.0.0</td>
3049<td><p><span>Deprecated since: v6.0.0</span></p></td></tr>
3050<tr><td>v3.0.0</td>
3051<td><p><span>Added in: v3.0.0</span></p></td></tr>
3052</tbody></table>
3053</details>
3054</div>
3055<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use
3056<a href="#buffer_static_method_buffer_from_arraybuffer_byteoffset_length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a>
3057instead.</div><p></p>
3058<ul>
3059<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>,
3060<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>
3061<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>
3062<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.
3063<strong>Default:</strong> <code>arrayBuffer.byteLength - byteOffset</code>.</li>
3064</ul>
3065<p>See
3066<a href="#buffer_static_method_buffer_from_arraybuffer_byteoffset_length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a>.</p>
3067<h4><code>new Buffer(buffer)</code><span><a class="mark" href="#buffer_new_buffer_buffer" id="buffer_new_buffer_buffer">#</a></span></h4>
3068<div class="api_metadata">
3069<details class="changelog"><summary>History</summary>
3070<table>
3071<tbody><tr><th>Version</th><th>Changes</th></tr>
3072<tr><td>v10.0.0</td>
3073<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr>
3074<tr><td>v7.2.1</td>
3075<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr>
3076<tr><td>v7.0.0</td>
3077<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr>
3078<tr><td>v6.0.0</td>
3079<td><p><span>Deprecated since: v6.0.0</span></p></td></tr>
3080</tbody></table>
3081</details>
3082</div>
3083<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#buffer_static_method_buffer_from_buffer"><code>Buffer.from(buffer)</code></a> instead.</div><p></p>
3084<ul>
3085<li><code>buffer</code> <a href="buffer.html#buffer_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
3086which to copy data.</li>
3087</ul>
3088<p>See <a href="#buffer_static_method_buffer_from_buffer"><code>Buffer.from(buffer)</code></a>.</p>
3089<h4><code>new Buffer(size)</code><span><a class="mark" href="#buffer_new_buffer_size" id="buffer_new_buffer_size">#</a></span></h4>
3090<div class="api_metadata">
3091<details class="changelog"><summary>History</summary>
3092<table>
3093<tbody><tr><th>Version</th><th>Changes</th></tr>
3094<tr><td>v10.0.0</td>
3095<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr>
3096<tr><td>v8.0.0</td>
3097<td><p>The <code>new Buffer(size)</code> will return zero-filled memory by default.</p></td></tr>
3098<tr><td>v7.2.1</td>
3099<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr>
3100<tr><td>v7.0.0</td>
3101<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr>
3102<tr><td>v6.0.0</td>
3103<td><p><span>Deprecated since: v6.0.0</span></p></td></tr>
3104</tbody></table>
3105</details>
3106</div>
3107<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#buffer_static_method_buffer_alloc_size_fill_encoding"><code>Buffer.alloc()</code></a> instead (also see
3108<a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a>).</div><p></p>
3109<ul>
3110<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>
3111</ul>
3112<p>See <a href="#buffer_static_method_buffer_alloc_size_fill_encoding"><code>Buffer.alloc()</code></a> and <a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a>. This variant of the
3113constructor is equivalent to <a href="#buffer_static_method_buffer_alloc_size_fill_encoding"><code>Buffer.alloc()</code></a>.</p>
3114<h4><code>new Buffer(string[, encoding])</code><span><a class="mark" href="#buffer_new_buffer_string_encoding" id="buffer_new_buffer_string_encoding">#</a></span></h4>
3115<div class="api_metadata">
3116<details class="changelog"><summary>History</summary>
3117<table>
3118<tbody><tr><th>Version</th><th>Changes</th></tr>
3119<tr><td>v10.0.0</td>
3120<td><p>Calling this constructor emits a deprecation warning when run from code outside the <code>node_modules</code> directory.</p></td></tr>
3121<tr><td>v7.2.1</td>
3122<td><p>Calling this constructor no longer emits a deprecation warning.</p></td></tr>
3123<tr><td>v7.0.0</td>
3124<td><p>Calling this constructor emits a deprecation warning now.</p></td></tr>
3125<tr><td>v6.0.0</td>
3126<td><p><span>Deprecated since: v6.0.0</span></p></td></tr>
3127</tbody></table>
3128</details>
3129</div>
3130<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated:
3131Use <a href="#buffer_static_method_buffer_from_string_encoding"><code>Buffer.from(string[, encoding])</code></a> instead.</div><p></p>
3132<ul>
3133<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>
3134<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>
3135</ul>
3136<p>See <a href="#buffer_static_method_buffer_from_string_encoding"><code>Buffer.from(string[, encoding])</code></a>.</p>
3137</section><section><h3><code>buffer</code> module APIs<span><a class="mark" href="#buffer_buffer_module_apis" id="buffer_buffer_module_apis">#</a></span></h3>
3138<p>While, the <code>Buffer</code> object is available as a global, there are additional
3139<code>Buffer</code>-related APIs that are available only via the <code>buffer</code> module
3140accessed using <code>require('buffer')</code>.</p>
3141<h4><code>buffer.atob(data)</code><span><a class="mark" href="#buffer_buffer_atob_data" id="buffer_buffer_atob_data">#</a></span></h4>
3142<div class="api_metadata">
3143<span>Added in: v14.17.0</span>
3144</div>
3145<ul>
3146<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>
3147</ul>
3148<p>Decodes a string of Base64-encoded data into bytes, and encodes those bytes
3149into a string using Latin-1 (ISO-8859-1).</p>
3150<p>The <code>data</code> may be any JavaScript-value that can be coerced into a string.</p>
3151<p><strong>This function is only provided for compatibility with legacy web platform APIs
3152and should never be used in new code, because they use strings to represent
3153binary data and predate the introduction of typed arrays in JavaScript.
3154For code running using Node.js APIs, converting between base64-encoded strings
3155and binary data should be performed using <code>Buffer.from(str, 'base64')</code> and
3156<code>buf.toString('base64')</code>.</strong></p>
3157<h4><code>buffer.btoa(data)</code><span><a class="mark" href="#buffer_buffer_btoa_data" id="buffer_buffer_btoa_data">#</a></span></h4>
3158<div class="api_metadata">
3159<span>Added in: v14.17.0</span>
3160</div>
3161<ul>
3162<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>
3163</ul>
3164<p>Decodes a string into bytes using Latin-1 (ISO-8859), and encodes those bytes
3165into a string using Base64.</p>
3166<p>The <code>data</code> may be any JavaScript-value that can be coerced into a string.</p>
3167<p><strong>This function is only provided for compatibility with legacy web platform APIs
3168and should never be used in new code, because they use strings to represent
3169binary data and predate the introduction of typed arrays in JavaScript.
3170For code running using Node.js APIs, converting between base64-encoded strings
3171and binary data should be performed using <code>Buffer.from(str, 'base64')</code> and
3172<code>buf.toString('base64')</code>.</strong></p>
3173<h4><code>buffer.INSPECT_MAX_BYTES</code><span><a class="mark" href="#buffer_buffer_inspect_max_bytes" id="buffer_buffer_inspect_max_bytes">#</a></span></h4>
3174<div class="api_metadata">
3175<span>Added in: v0.5.4</span>
3176</div>
3177<ul>
3178<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>
3179</ul>
3180<p>Returns the maximum number of bytes that will be returned when
3181<code>buf.inspect()</code> is called. This can be overridden by user modules. See
3182<a href="util.html#util_util_inspect_object_options"><code>util.inspect()</code></a> for more details on <code>buf.inspect()</code> behavior.</p>
3183<h4><code>buffer.kMaxLength</code><span><a class="mark" href="#buffer_buffer_kmaxlength" id="buffer_buffer_kmaxlength">#</a></span></h4>
3184<div class="api_metadata">
3185<span>Added in: v3.0.0</span>
3186</div>
3187<ul>
3188<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>
3189</ul>
3190<p>An alias for <a href="#buffer_buffer_constants_max_length"><code>buffer.constants.MAX_LENGTH</code></a>.</p>
3191<h4><code>buffer.kStringMaxLength</code><span><a class="mark" href="#buffer_buffer_kstringmaxlength" id="buffer_buffer_kstringmaxlength">#</a></span></h4>
3192<div class="api_metadata">
3193<span>Added in: v3.0.0</span>
3194</div>
3195<ul>
3196<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>
3197</ul>
3198<p>An alias for <a href="#buffer_buffer_constants_max_string_length"><code>buffer.constants.MAX_STRING_LENGTH</code></a>.</p>
3199<h4><code>buffer.transcode(source, fromEnc, toEnc)</code><span><a class="mark" href="#buffer_buffer_transcode_source_fromenc_toenc" id="buffer_buffer_transcode_source_fromenc_toenc">#</a></span></h4>
3200<div class="api_metadata">
3201<details class="changelog"><summary>History</summary>
3202<table>
3203<tbody><tr><th>Version</th><th>Changes</th></tr>
3204<tr><td>v8.0.0</td>
3205<td><p>The <code>source</code> parameter can now be a <code>Uint8Array</code>.</p></td></tr>
3206<tr><td>v7.1.0</td>
3207<td><p><span>Added in: v7.1.0</span></p></td></tr>
3208</tbody></table>
3209</details>
3210</div>
3211<ul>
3212<li><code>source</code> <a href="buffer.html#buffer_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>
3213<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>
3214<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>
3215<li>Returns: <a href="buffer.html#buffer_class_buffer" class="type">&#x3C;Buffer></a></li>
3216</ul>
3217<p>Re-encodes the given <code>Buffer</code> or <code>Uint8Array</code> instance from one character
3218encoding to another. Returns a new <code>Buffer</code> instance.</p>
3219<p>Throws if the <code>fromEnc</code> or <code>toEnc</code> specify invalid character encodings or if
3220conversion from <code>fromEnc</code> to <code>toEnc</code> is not permitted.</p>
3221<p>Encodings supported by <code>buffer.transcode()</code> are: <code>'ascii'</code>, <code>'utf8'</code>,
3222<code>'utf16le'</code>, <code>'ucs2'</code>, <code>'latin1'</code>, and <code>'binary'</code>.</p>
3223<p>The transcoding process will use substitution characters if a given byte
3224sequence cannot be adequately represented in the target encoding. For instance:</p>
3225<pre><code class="language-js"><span class="hljs-keyword">const</span> buffer = <span class="hljs-built_in">require</span>(<span class="hljs-string">'buffer'</span>);
3226
3227<span class="hljs-keyword">const</span> newBuf = buffer.<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>);
3228<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>));
3229<span class="hljs-comment">// Prints: '?'</span></code></pre>
3230<p>Because the Euro (<code>€</code>) sign is not representable in US-ASCII, it is replaced
3231with <code>?</code> in the transcoded <code>Buffer</code>.</p>
3232<h4>Class: <code>SlowBuffer</code><span><a class="mark" href="#buffer_class_slowbuffer" id="buffer_class_slowbuffer">#</a></span></h4>
3233<div class="api_metadata">
3234<span>Deprecated since: v6.0.0</span>
3235</div>
3236<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#buffer_static_method_buffer_allocunsafeslow_size"><code>Buffer.allocUnsafeSlow()</code></a> instead.</div><p></p>
3237<p>See <a href="#buffer_static_method_buffer_allocunsafeslow_size"><code>Buffer.allocUnsafeSlow()</code></a>. This was never a class in the sense that
3238the constructor always returned a <code>Buffer</code> instance, rather than a <code>SlowBuffer</code>
3239instance.</p>
3240<h5><code>new SlowBuffer(size)</code><span><a class="mark" href="#buffer_new_slowbuffer_size" id="buffer_new_slowbuffer_size">#</a></span></h5>
3241<div class="api_metadata">
3242<span>Deprecated since: v6.0.0</span>
3243</div>
3244<p></p><div class="api_stability api_stability_0"><a href="documentation.html#documentation_stability_index">Stability: 0</a> - Deprecated: Use <a href="#buffer_static_method_buffer_allocunsafeslow_size"><code>Buffer.allocUnsafeSlow()</code></a> instead.</div><p></p>
3245<ul>
3246<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>
3247</ul>
3248<p>See <a href="#buffer_static_method_buffer_allocunsafeslow_size"><code>Buffer.allocUnsafeSlow()</code></a>.</p>
3249<h4>Buffer constants<span><a class="mark" href="#buffer_buffer_constants" id="buffer_buffer_constants">#</a></span></h4>
3250<div class="api_metadata">
3251<span>Added in: v8.2.0</span>
3252</div>
3253<h5><code>buffer.constants.MAX_LENGTH</code><span><a class="mark" href="#buffer_buffer_constants_max_length" id="buffer_buffer_constants_max_length">#</a></span></h5>
3254<div class="api_metadata">
3255<details class="changelog"><summary>History</summary>
3256<table>
3257<tbody><tr><th>Version</th><th>Changes</th></tr>
3258<tr><td>v14.0.0</td>
3259<td><p>Value is changed from 2<sup>31</sup> - 1 to 2<sup>32</sup> - 1 on 64-bit architectures.</p></td></tr>
3260<tr><td>v8.2.0</td>
3261<td><p><span>Added in: v8.2.0</span></p></td></tr>
3262</tbody></table>
3263</details>
3264</div>
3265<ul>
3266<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>
3267</ul>
3268<p>On 32-bit architectures, this value currently is 2<sup>30</sup> - 1 (~1GB).</p>
3269<p>On 64-bit architectures, this value currently is 2<sup>32</sup> - 1 (~4GB).</p>
3270<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>
3271<p>This value is also available as <a href="#buffer_buffer_kmaxlength"><code>buffer.kMaxLength</code></a>.</p>
3272<h5><code>buffer.constants.MAX_STRING_LENGTH</code><span><a class="mark" href="#buffer_buffer_constants_max_string_length" id="buffer_buffer_constants_max_string_length">#</a></span></h5>
3273<div class="api_metadata">
3274<span>Added in: v8.2.0</span>
3275</div>
3276<ul>
3277<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>
3278</ul>
3279<p>Represents the largest <code>length</code> that a <code>string</code> primitive can have, counted
3280in UTF-16 code units.</p>
3281<p>This value may depend on the JS engine that is being used.</p>
3282</section><section><h3><code>Buffer.from()</code>, <code>Buffer.alloc()</code>, and <code>Buffer.allocUnsafe()</code><span><a class="mark" href="#buffer_buffer_from_buffer_alloc_and_buffer_allocunsafe" id="buffer_buffer_from_buffer_alloc_and_buffer_allocunsafe">#</a></span></h3>
3283<p>In versions of Node.js prior to 6.0.0, <code>Buffer</code> instances were created using the
3284<code>Buffer</code> constructor function, which allocates the returned <code>Buffer</code>
3285differently based on what arguments are provided:</p>
3286<ul>
3287<li>Passing a number as the first argument to <code>Buffer()</code> (e.g. <code>new Buffer(10)</code>)
3288allocates a new <code>Buffer</code> object of the specified size. Prior to Node.js 8.0.0,
3289the memory allocated for such <code>Buffer</code> instances is <em>not</em> initialized and
3290<em>can contain sensitive data</em>. Such <code>Buffer</code> instances <em>must</em> be subsequently
3291initialized by using either <a href="#buffer_buf_fill_value_offset_end_encoding"><code>buf.fill(0)</code></a> or by writing to the
3292entire <code>Buffer</code> before reading data from the <code>Buffer</code>.
3293While this behavior is <em>intentional</em> to improve performance,
3294development experience has demonstrated that a more explicit distinction is
3295required between creating a fast-but-uninitialized <code>Buffer</code> versus creating a
3296slower-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>
3297<li>Passing a string, array, or <code>Buffer</code> as the first argument copies the
3298passed object's data into the <code>Buffer</code>.</li>
3299<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>
3300that shares allocated memory with the given array buffer.</li>
3301</ul>
3302<p>Because the behavior of <code>new Buffer()</code> is different depending on the type of the
3303first argument, security and reliability issues can be inadvertently introduced
3304into applications when argument validation or <code>Buffer</code> initialization is not
3305performed.</p>
3306<p>For example, if an attacker can cause an application to receive a number where
3307a string is expected, the application may call <code>new Buffer(100)</code>
3308instead of <code>new Buffer("100")</code>, leading it to allocate a 100 byte buffer instead
3309of allocating a 3 byte buffer with content <code>"100"</code>. This is commonly possible
3310using JSON API calls. Since JSON distinguishes between numeric and string types,
3311it allows injection of numbers where a naively written application that does not
3312validate its input sufficiently might expect to always receive a string.
3313Before Node.js 8.0.0, the 100 byte buffer might contain
3314arbitrary pre-existing in-memory data, so may be used to expose in-memory
3315secrets to a remote attacker. Since Node.js 8.0.0, exposure of memory cannot
3316occur because the data is zero-filled. However, other attacks are still
3317possible, such as causing very large buffers to be allocated by the server,
3318leading to performance degradation or crashing on memory exhaustion.</p>
3319<p>To make the creation of <code>Buffer</code> instances more reliable and less error-prone,
3320the various forms of the <code>new Buffer()</code> constructor have been <strong>deprecated</strong>
3321and replaced by separate <code>Buffer.from()</code>, <a href="#buffer_static_method_buffer_alloc_size_fill_encoding"><code>Buffer.alloc()</code></a>, and
3322<a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a> methods.</p>
3323<p><em>Developers should migrate all existing uses of the <code>new Buffer()</code> constructors
3324to one of these new APIs.</em></p>
3325<ul>
3326<li><a href="#buffer_static_method_buffer_from_array"><code>Buffer.from(array)</code></a> returns a new <code>Buffer</code> that <em>contains a copy</em> of the
3327provided octets.</li>
3328<li><a href="#buffer_static_method_buffer_from_arraybuffer_byteoffset_length"><code>Buffer.from(arrayBuffer[, byteOffset[, length]])</code></a>
3329returns a new <code>Buffer</code> that <em>shares the same allocated memory</em> as the given
3330<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer"><code>ArrayBuffer</code></a>.</li>
3331<li><a href="#buffer_static_method_buffer_from_buffer"><code>Buffer.from(buffer)</code></a> returns a new <code>Buffer</code> that <em>contains a copy</em> of the
3332contents of the given <code>Buffer</code>.</li>
3333<li><a href="#buffer_static_method_buffer_from_string_encoding"><code>Buffer.from(string[, encoding])</code></a> returns a new
3334<code>Buffer</code> that <em>contains a copy</em> of the provided string.</li>
3335<li><a href="#buffer_static_method_buffer_alloc_size_fill_encoding"><code>Buffer.alloc(size[, fill[, encoding]])</code></a> returns a new
3336initialized <code>Buffer</code> of the specified size. This method is slower than
3337<a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe(size)</code></a> but guarantees that newly
3338created <code>Buffer</code> instances never contain old data that is potentially
3339sensitive. A <code>TypeError</code> will be thrown if <code>size</code> is not a number.</li>
3340<li><a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe(size)</code></a> and
3341<a href="#buffer_static_method_buffer_allocunsafeslow_size"><code>Buffer.allocUnsafeSlow(size)</code></a> each return a
3342new uninitialized <code>Buffer</code> of the specified <code>size</code>. Because the <code>Buffer</code> is
3343uninitialized, the allocated segment of memory might contain old data that is
3344potentially sensitive.</li>
3345</ul>
3346<p><code>Buffer</code> instances returned by <a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a> and
3347<a href="#buffer_static_method_buffer_from_array"><code>Buffer.from(array)</code></a> <em>may</em> be allocated off a shared internal memory pool
3348if <code>size</code> is less than or equal to half <a href="#buffer_class_property_buffer_poolsize"><code>Buffer.poolSize</code></a>. Instances
3349returned by <a href="#buffer_static_method_buffer_allocunsafeslow_size"><code>Buffer.allocUnsafeSlow()</code></a> <em>never</em> use the shared internal
3350memory pool.</p>
3351<h4>The <code>--zero-fill-buffers</code> command-line option<span><a class="mark" href="#buffer_the_zero_fill_buffers_command_line_option" id="buffer_the_zero_fill_buffers_command_line_option">#</a></span></h4>
3352<div class="api_metadata">
3353<span>Added in: v5.10.0</span>
3354</div>
3355<p>Node.js can be started using the <code>--zero-fill-buffers</code> command-line option to
3356cause all newly-allocated <code>Buffer</code> instances to be zero-filled upon creation by
3357default. Without the option, buffers created with <a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a>,
3358<a href="#buffer_static_method_buffer_allocunsafeslow_size"><code>Buffer.allocUnsafeSlow()</code></a>, and <code>new SlowBuffer(size)</code> are not zero-filled.
3359Use of this flag can have a measurable negative impact on performance. Use the
3360<code>--zero-fill-buffers</code> option only when necessary to enforce that newly allocated
3361<code>Buffer</code> instances cannot contain old data that is potentially sensitive.</p>
3362<pre><code class="language-console"><span class="hljs-meta">$ </span><span class="language-bash">node --zero-fill-buffers</span>
3363<span class="hljs-meta">> </span><span class="language-bash">Buffer.allocUnsafe(5);</span>
3364&#x3C;Buffer 00 00 00 00 00></code></pre>
3365<h4>What makes <code>Buffer.allocUnsafe()</code> and <code>Buffer.allocUnsafeSlow()</code> "unsafe"?<span><a class="mark" href="#buffer_what_makes_buffer_allocunsafe_and_buffer_allocunsafeslow_unsafe" id="buffer_what_makes_buffer_allocunsafe_and_buffer_allocunsafeslow_unsafe">#</a></span></h4>
3366<p>When calling <a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a> and <a href="#buffer_static_method_buffer_allocunsafeslow_size"><code>Buffer.allocUnsafeSlow()</code></a>, the
3367segment of allocated memory is <em>uninitialized</em> (it is not zeroed-out). While
3368this design makes the allocation of memory quite fast, the allocated segment of
3369memory might contain old data that is potentially sensitive. Using a <code>Buffer</code>
3370created by <a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a> without <em>completely</em> overwriting the
3371memory can allow this old data to be leaked when the <code>Buffer</code> memory is read.</p>
3372<p>While there are clear performance advantages to using
3373<a href="#buffer_static_method_buffer_allocunsafe_size"><code>Buffer.allocUnsafe()</code></a>, extra care <em>must</em> be taken in order to avoid
3374introducing security vulnerabilities into an application.</p></section>
3375        <!-- API END -->
3376      </div>
3377    </div>
3378  </div>
3379  <script>
3380    'use strict';
3381    {
3382      const kCustomPreference = 'customDarkTheme';
3383      const userSettings = sessionStorage.getItem(kCustomPreference);
3384      const themeToggleButton = document.getElementById('theme-toggle-btn');
3385      if (userSettings === null && window.matchMedia) {
3386        const mq = window.matchMedia('(prefers-color-scheme: dark)');
3387        if ('onchange' in mq) {
3388          function mqChangeListener(e) {
3389            document.body.classList.toggle('dark-mode', e.matches);
3390          }
3391          mq.addEventListener('change', mqChangeListener);
3392          if (themeToggleButton) {
3393            themeToggleButton.addEventListener('click', function() {
3394              mq.removeEventListener('change', mqChangeListener);
3395            }, { once: true });
3396          }
3397        }
3398        if (mq.matches) {
3399          document.body.classList.add('dark-mode');
3400        }
3401      } else if (userSettings === 'true') {
3402        document.body.classList.add('dark-mode');
3403      }
3404      if (themeToggleButton) {
3405        themeToggleButton.hidden = false;
3406        themeToggleButton.addEventListener('click', function() {
3407          sessionStorage.setItem(
3408            kCustomPreference,
3409            document.body.classList.toggle('dark-mode')
3410          );
3411        });
3412      }
3413    }
3414  </script>
3415</body>
3416</html>
3417