Just a few weeks until the 2021 JavaScript Full-Stack Bootcamp opens.
Signup to the waiting list!
JavaScript Provides 8 Typed Array types:
Int8Array
an array of 8-bit signed integersInt16Array
an array of 16-bit signed integersInt32Array
an array of 32-bit signed integersUint8Array
an array of 8-bit unsigned integersUint16Array
an array of 16-bit unsigned integersUint32Array
an array of 32-bit unsigned integersFloat32Array
an array of 32-bit floating point numbersFloat64Array
an array of 64-bit floating point numbers
all of them are ArrayBufferView
instances.
A Typed Array is essentially a view into an ArrayBuffer
, where every item has the same size, and type.
DataView
is another view into an ArrayBuffer, but in this case the items in the array can have different sizes and types.
Here’s an example of how to create an array of 8-bit signed integers:
const a = new Int8Array()
You can pre-allocate n bytes:
const bytes = 1024
const a = new Int8Array(bytes)
The main use is to allow to look into an ArrayBuffer, which on its own is opaque (we can’t inspect its content).
Here’s how we do so:
//we got this `buffer` ArrayBuffer
const a = new Int8Array(buffer)
Those typed arrays are array-like, so now we can inspect the content of the buffer via the usual array access techniques, and we have access to lots of methods and properties including map()
, reduce()
and so on.
The main use case for Typed Arrays is to use with WebGL, Web Audio or the Canvas API. Some of the WebGL functions are expecting typed arrays, as they are much more performant than regular JavaScript arrays.
One thing to keep in mind is that typed arrays don’t let us control the endianness: it uses the byte order of the platform. In general this works out fine, because the main use case as we said is to use the array locally, using one of the multimedia APIs. Also, most consumer computers use little endian since Intel uses that convention. But, if you transfer the data of a Typed Array on a system that uses big endian, the data might be badly encoded and, as such, invalid.
In case you need this kind of control over endianness, use DataView instead.
Download my free JavaScript Beginner's Handbook
The 2021 JavaScript Full-Stack Bootcamp will start at the end of March 2021. Don't miss this opportunity, signup to the waiting list!
More browser tutorials:
- Some useful tricks available in HTML5
- How I made a CMS-based website work offline
- The Complete Guide to Progressive Web Apps
- The Fetch API
- The Push API Guide
- The Channel Messaging API
- Service Workers Tutorial
- The Cache API Guide
- The Notification API Guide
- Dive into IndexedDB
- The Selectors API: querySelector and querySelectorAll
- Efficiently load JavaScript with defer and async
- The Document Object Model (DOM)
- The Web Storage API: local storage and session storage
- Learn how HTTP Cookies work
- The History API
- The WebP Image Format
- XMLHttpRequest (XHR)
- An in-depth SVG tutorial
- What are Data URLs
- Roadmap to learn the Web Platform
- CORS, Cross-Origin Resource Sharing
- Web Workers
- The requestAnimationFrame() guide
- What is the Doctype
- Working with the DevTools Console and the Console API
- The Speech Synthesis API
- How to wait for the DOM ready event in plain JavaScript
- How to add a class to a DOM element
- How to loop over DOM elements from querySelectorAll
- How to remove a class from a DOM element
- How to check if a DOM element has a class
- How to change a DOM node value
- How to add a click event to a list of DOM elements returned from querySelectorAll
- WebRTC, the Real Time Web API
- How to get the scroll position of an element in JavaScript
- How to replace a DOM element
- How to only accept images in an input file field
- Why use a preview version of a browser?
- The Blob Object
- The File Object
- The FileReader Object
- The FileList Object
- ArrayBuffer
- ArrayBufferView
- The URL Object
- Typed Arrays
- The DataView Object
- The BroadcastChannel API
- The Streams API
- The FormData Object
- The Navigator Object
- How to use the Geolocation API
- How to use getUserMedia()
- How to use the Drag and Drop API
- How to work with scrolling on Web Pages
- Handling forms in JavaScript
- Keyboard events
- Mouse events
- Touch events
- How to remove all children from a DOM element
- How to create an HTML attribute using vanilla Javascript
- How to check if a checkbox is checked using JavaScript?
- How to copy to the clipboard using JavaScript
- How to disable a button using JavaScript
- How to make a page editable in the browser
- How to get query string values in JavaScript with URLSearchParams
- How to remove all CSS from a page at once
- How to use insertAdjacentHTML
- Safari, warn before quitting
- How to add an image to the DOM using JavaScript
- How to reset a form
- How to use Google Fonts