Skip to content

The DataView Object

Find out what is a DataView object and how to use it

DataView is a view into an ArrayBuffer, like Typed Arrays, but in this case the items in the array can have different sizes and types.

Here’s an example:

const buffer = new ArrayBuffer(64)
const view = new DataView(buffer)

Since this is a view over a buffer, we can specify which byte we want to start from, and the length:

const view = new DataView(buffer, 10) //start at byte 10
const view = new DataView(buffer, 10, 30) //start at byte 10, and add 30 items

If we don’t add those additional arguments, the view starts at position 0 and loads all the bytes present in the buffer.

There is a set of methods we can use to add data into the buffer:

This is how to call one of those methods:

const buffer = new ArrayBuffer(64)
const view = new DataView(buffer)
view.setInt16(0, 2019)

By default data is stored using big endian notation. You can overwrite this setting and use little endian by adding a third parameter with the true value:

const buffer = new ArrayBuffer(64)
const view = new DataView(buffer)
view.setInt16(0, 2019, true)

Here is how we can get data from the view:

Example:

const buffer = new ArrayBuffer(64)
const view = new DataView(buffer)
view.setInt16(0, 2019)
view.getInt16(0) //2019

Since a DataView is an ArrayBufferView, we have those 3 read-only properties:

One thing to keep in mind is that typed arrays don’t let us control the endianness: it uses the endianness of the system. 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.

If you transfer the data of a Typed Array on another system, the data might be not badly encoded if it uses Big Endian and you use Little Endian.

In case you need this kind of control, DataView is a perfect choice.


download all my books for free

  • javascript handbook
  • typescript handbook
  • css handbook
  • node.js handbook
  • astro handbook
  • html handbook
  • next.js pages router handbook
  • alpine.js handbook
  • htmx handbook
  • react handbook
  • sql handbook
  • git cheat sheet
  • laravel handbook
  • express handbook
  • swift handbook
  • go handbook
  • php handbook
  • python handbook
  • cli handbook
  • c handbook

subscribe to my newsletter to get them

Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.

Related posts about platform: