By using typed arrays, you can store arrays of these types:

Type Value Range Size(bytes)
Int8Array -128 to 127 1
Uint8Array 0 to 255 1
Uint8ClampedArray 0 to 255 1
Int16Array -32768 to 32767 2
Uint16Array 0 to 65535 2
Int32Array -2147483648 to 2147483647 4
Uint32Array 0 to 4294967295 4
Float32Array -3.4E38 to 3.4E38 4
Float64Array -1.8E308 to 1.8E308 8
BigInt64Array -2^63 to 2^63 - 1 8
BigUint64Array 0 to 2^64 - 1 8

Demo in Stack Snippets & JSFiddle

var array = new Uint8Array(100);
array[42] = 10;
console.log(array[42]);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Answer from Kendall Frey on Stack Overflow
Discussions

How can I create byte array in js?
When I implement this function as js code, I don't know how to create a byte array. The function read of InputStream need byte array as first arg. More on github.com
🌐 github.com
10
June 26, 2017
[JavaScript] What is a byte? How do the Uint8Array and TextEncoder/TextDecoder functions relate to it?
For a proper answer that will let you understand this topic in depth you would need to look into a different programming language like C. Javascript is a bit too high level (meaning it hides details from you) so you are left with incomplete picture. That being said - are you familiar with binary system? Aka 0s and 1s. A single byte is built of 8 bits (aka individual digits). For instance 11 is 3 (20 + 21 ), 11011 is 27 (1+2+8+16). Meaning that byte can have a maximum value of 255 (11111111, 1+2+4+8+16+32+64+128). Now, integers are much bigger than individual bytes. This depends HEAVILY on a platform you are working on (64-bit vs 32-bit system, little microcomputers vs full fledged PCs) but in general you can expect Integer to hold at least 4 bytes in most situations. That's up to 11111111 11111111 11111111 11111111 (4294967295 in our human friendly system). This means that a single integer takes 4x as much space in memory as a single byte. Why is this important? Because memory concerns while much lower nowadays are still not to be ignored. Example - you have a FullHD movie. That's 1920x1080 pixels (2073600 total). Now, a single pixel can be defined with values RGB (red, green, blue) ranging from 0 to 255. Meaning that without any compression a frame takes 2073600 x 3 bytes = 6220800 bytes. That's 5 MB/s per frame or 150MB/s (assuming 30 fps). If you used integers for some reason (and only used their first byte since max value is still 255, just that it looks like 00000000 00000000 00000000 11111111 in memory) you now need 3x more memory. Realistically it could go even higher cuz save for colours you might also have an Alpha (transparency) layer. As for text: Does it look at every X numbers and perform an operation? With ASCII it would be the case. Here's ASCII: http://www.asciitable.com/ Long story short - each character IS actually a number to a computer. We just chose to display value of 65 as 'A' for instance. You might as well make up your own system that takes an integer and prints 'F' when it's value is 30. But this would be insanely messy if everyone tried that so we have made official standards over the years. It's actually not that simple as individual characters in UTF8 are NOT of fixed size. In fact there are 3 specific ways on looking at characters: one is at individual bytes second is scalar values. This is what a character is made of. last is graphene clusters. This is what we humans consider to be complete characters. With english it's not that important but well, UTF-8 is supposed to take all characters existing, not just english ones. Here's a somewhat trollish example: 💩 This thing is actually made of more than one individual byte. Alone they don't make sense and can't even be displayed (you can't even save a reddit post with them separately), only together they create poo. Another sample is नमस्ते. Which consists of byte values [224, 164, 168, 224, 164, 174, 224, 164, 184, 224, 165, 141, 224, 164, 164,224, 165, 135] and scalar values ['न', 'म', 'स', '्', 'त', 'े'] (I shamelessly used Rust programming language explanation from here: https://doc.rust-lang.org/book/second-edition/ch08-02-strings.html ). Here's a good explanation on UTF-8: https://www.youtube.com/watch?v=MijmeoH9LT4 But let's just put it this way - there's a lot of magic going behind your back when it comes to characters encoding. Javascript hides a lot of it from you (which is why I said at the very beginning you would need to try out a lower level one) but sometimes it has to expose these functions to you directly. More on reddit.com
🌐 r/learnprogramming
10
2
December 27, 2017
construct-js: A library for creating byte level data structures
I’m about to grossly misuse JavaScript with this in hand. More on reddit.com
🌐 r/javascript
14
53
September 28, 2021
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Typed_arrays
JavaScript typed arrays - JavaScript | MDN
The length, in bytes, of the view. Both constructors accept the above three as separate arguments, although typed array constructors accept length as the number of elements rather than the number of bytes.
🌐
Substack
alexanderobregon.substack.com › alexander obregon's substack › reading binary data in javascript with typed arrays and arraybuffer
Reading Binary Data in JavaScript with Typed Arrays and ArrayBuffer
June 25, 2025 - Everything starts with memory, and in JavaScript, ArrayBuffer is the block you use to hold raw bytes. It doesn’t carry any structure on its own. You decide how to read and write the contents by attaching typed views that shape those bytes into real values. The buffer gives you a fixed-size chunk, and once it’s set, the size doesn’t change.
🌐
W3Schools
w3schools.com › js › js_typed_arrays.asp
JavaScript Typed Arrays
Typed Arrays were designed to provide an efficient way to handle binary data, unlike traditional JavaScript arrays which can hold elements of mixed data types.
Find elsewhere
🌐
Reddit
reddit.com › r/javascripttips › how to convert string to byte array in javascript?
r/JavaScriptTips on Reddit: How to Convert String to Byte Array in JavaScript?
May 1, 2023 - This object provides a view of an underlying ArrayBuffer object, which represents a fixed-length, contiguous block of memory. To convert a JavaScript string to a byte array, we can use the TextEncoder interface.
🌐
Medium
medium.com › @julienetienne › different-types-of-arrays-in-javascript-and-when-to-use-them-77f7843b71de
Different Types of Arrays in JavaScript + When to Use Them | by Julien Etienne | Medium
February 8, 2024 - An ArrayBuffer is a “Byte Array”, which is an area of memory containing a group of contiguous (one after the other) bytes.
🌐
JavaScript in Plain English
javascript.plainenglish.io › javascript-data-encoding-and-decoding-techniques-buffers-byte-arrays-dataview-binary-219d55b535a2
JavaScript Data Encoding and Decoding Techniques: Buffers, Byte arrays, DataView, binary, hexadecimal, and decimals. | by Varun Kukade | JavaScript in Plain English
May 4, 2024 - Now we can accept the byte array and convert it to an appropriate format to read the data. In JavaScript, unlike some other programming languages such as Java, bytes are not explicitly defined as a primitive data type. Instead, JavaScript has a generic number data type.
🌐
GitHub
github.com › frida › frida › issues › 293
How can I create byte array in js? · Issue #293 · frida/frida
June 26, 2017 - I use frida to hook java application and I want to overload the following code of Java: void java_func(HttpURLConnection httpconne){ InputStream input = httpconne.getInputStream(); ByteArrayOutputStream baos = new byteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while (( len = input.read(buffer)) > -1) { baos.write(buffer,0,len); } baos.flush(); }
Author: frida
🌐
Coderanch
coderanch.com › t › 528355 › languages › Displaying-byte-array-image-javascript
Displaying byte array as image using javascript (HTML Pages with CSS and JavaScript forum at Coderanch)
I am retrieving all the table values using json array and displaying using javascript, in the json array i am adding this image byte array also. I am able to retrieve the other values, but not able to display this byte array as image.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Uint8Array
Uint8Array - JavaScript | MDN
Populates this Uint8Array object with bytes from a hex-encoded string, returning an object indicating how many bytes were read and written.
🌐
JavaScript.info
javascript.info › tutorial › binary data, files
ArrayBuffer, binary arrays
July 11, 2022 - Binary data in JavaScript is implemented in a non-standard way, compared to other languages. But when we sort things out, everything becomes fairly simple. The basic binary object is ArrayBuffer – a reference to a fixed-length contiguous memory area. ... This allocates a contiguous memory area of 16 bytes and pre-fills it with zeroes.
🌐
Reddit
reddit.com › r/learnprogramming › [javascript] what is a byte? how do the uint8array and textencoder/textdecoder functions relate to it?
r/learnprogramming on Reddit: [JavaScript] What is a byte? How do the Uint8Array and TextEncoder/TextDecoder functions relate to it?
December 27, 2017 -

Hello,

I am trying very hard to understand these things, but the confusing articles online aimed at people with CS experience aren't helping.

1.) I am told that a Uint8Array and similar data types contain a list of "bytes". How is this different than a normal list of numbers?

2.) What does it mean to convert this "array of bytes" (which seems be just an array of integers) using TextDecoder.decode?

  • Going deeper, what even is a decoder/encoder? If I use TextDecoder.decode to decode an array of bytes as UTF-8 and get a string back, what in the world is actually going on? Does it look at every X numbers and perform an operation?

Any resources on this would be much appreciate too.

Thanks!

Top answer
1 of 3
4
For a proper answer that will let you understand this topic in depth you would need to look into a different programming language like C. Javascript is a bit too high level (meaning it hides details from you) so you are left with incomplete picture. That being said - are you familiar with binary system? Aka 0s and 1s. A single byte is built of 8 bits (aka individual digits). For instance 11 is 3 (20 + 21 ), 11011 is 27 (1+2+8+16). Meaning that byte can have a maximum value of 255 (11111111, 1+2+4+8+16+32+64+128). Now, integers are much bigger than individual bytes. This depends HEAVILY on a platform you are working on (64-bit vs 32-bit system, little microcomputers vs full fledged PCs) but in general you can expect Integer to hold at least 4 bytes in most situations. That's up to 11111111 11111111 11111111 11111111 (4294967295 in our human friendly system). This means that a single integer takes 4x as much space in memory as a single byte. Why is this important? Because memory concerns while much lower nowadays are still not to be ignored. Example - you have a FullHD movie. That's 1920x1080 pixels (2073600 total). Now, a single pixel can be defined with values RGB (red, green, blue) ranging from 0 to 255. Meaning that without any compression a frame takes 2073600 x 3 bytes = 6220800 bytes. That's 5 MB/s per frame or 150MB/s (assuming 30 fps). If you used integers for some reason (and only used their first byte since max value is still 255, just that it looks like 00000000 00000000 00000000 11111111 in memory) you now need 3x more memory. Realistically it could go even higher cuz save for colours you might also have an Alpha (transparency) layer. As for text: Does it look at every X numbers and perform an operation? With ASCII it would be the case. Here's ASCII: http://www.asciitable.com/ Long story short - each character IS actually a number to a computer. We just chose to display value of 65 as 'A' for instance. You might as well make up your own system that takes an integer and prints 'F' when it's value is 30. But this would be insanely messy if everyone tried that so we have made official standards over the years. It's actually not that simple as individual characters in UTF8 are NOT of fixed size. In fact there are 3 specific ways on looking at characters: one is at individual bytes second is scalar values. This is what a character is made of. last is graphene clusters. This is what we humans consider to be complete characters. With english it's not that important but well, UTF-8 is supposed to take all characters existing, not just english ones. Here's a somewhat trollish example: 💩 This thing is actually made of more than one individual byte. Alone they don't make sense and can't even be displayed (you can't even save a reddit post with them separately), only together they create poo. Another sample is नमस्ते. Which consists of byte values [224, 164, 168, 224, 164, 174, 224, 164, 184, 224, 165, 141, 224, 164, 164,224, 165, 135] and scalar values ['न', 'म', 'स', '्', 'त', 'े'] (I shamelessly used Rust programming language explanation from here: https://doc.rust-lang.org/book/second-edition/ch08-02-strings.html ). Here's a good explanation on UTF-8: https://www.youtube.com/watch?v=MijmeoH9LT4 But let's just put it this way - there's a lot of magic going behind your back when it comes to characters encoding. Javascript hides a lot of it from you (which is why I said at the very beginning you would need to try out a lower level one) but sometimes it has to expose these functions to you directly.
2 of 3
2
https://www.google.com/search?q=Uint8Array
🌐
Node.js
nodejs.org › api › buffer.html
Buffer | Node.js v26.10.0 Documentation
Returns a promise that fulfills with an <ArrayBuffer> containing a copy of the Blob data. ... The blob.bytes() method returns the byte of the Blob object as a Promise<Uint8Array>.
🌐
Reddit
reddit.com › r/javascript › construct-js: a library for creating byte level data structures
r/javascript on Reddit: construct-js: A library for creating byte level data structures
September 28, 2021 - construct-js keeps all the data passed to it (numbers, bigints, strings, arrays of the aforementioned) in memory, and only renders that into a Uint8Array when you call structOrField.toUint8Array() - so there's at least twice as much data around in memory. You could conceivably force all of the original values to get garbage collected by letting them fall out of scope, and only keeping the final byte array, but you'd still incur the cost while building it up.
🌐
Medium
medium.com › @marcokuoni › javascript-and-bytes-44a70871986
JavaScript and Bytes. Communication with the outer world. To… | by Marco Kuoni | Medium
August 26, 2023 - JavaScript abstracts the memory and the data types. For instance, if we take the standard array type, its individual elements can possess various data types, and their representation in memory is not defined. This applies not only to complex data types but also to simple ones like number. However, when communicating through certain APIs (filesystem, web workers, WebAssembly, etc.), the byte arrangement must be guaranteed during data exchange.
🌐
npm
npmjs.com › search
byte array - npm search
Simple byte-array conversion utility. ... kruithne• 1.0.5 • 9 years ago • 2 dependents • MITpublished version 1.0.5, 9 years ago2 dependents licensed under $MIT ... Conversion of JavaScript primitives to and from Buffer with binary order matching natural primitive order
🌐
Google Groups
groups.google.com › g › haxelang › c › 1w6HJR1tGUk
javascript: Uint8array vs. Bytes
import haxe.io.Bytes; import haxe.io.UInt8Array; import haxe.io.UInt16Array; class Test { static function main() { var data8 = UInt8Array.fromArray([for (i in 0...10) i]); var data16:UInt16Array = cast data8; var bytes:Bytes = data8.view.buffer; trace(data8[0]); trace(data16[0]); trace(bytes.get(0)); bytes.set(0, 0xFF); trace(data8[0]); trace(data16[0]); trace(bytes.get(0)); } }
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › TypedArray › byteLength
TypedArray.prototype.byteLength - JavaScript | MDN
September 25, 2025 - The byteLength accessor property of TypedArray instances returns the length (in bytes) of this typed array.