• JavaScript Video Tutorials

JavaScript - DataView



The DataView is an object in JavaScript that allows you to work with the binary data stored in the ArrayBuffer. It provides a low-level interface for reading and writing number types in a binary ArrayBuffer.

The DataView object provides built-in methods for reading and writing 1, 2, and 4-byte signed and unsigned integers, as well as 4 and 8-byte floating-point numbers from the buffer.

An ArrayBuffer is an array of bytes, usually referred to in other languages as a "byte array". You cannot directly manipulate the data of an ArrayBuffer like a regular array. Using the ArrayBuffer you can create DataView object, which represents the buffer in a specific format. You can use the DataView object to read from and write to the contents of the buffer.

Syntax

Following is the syntax to create a DataView object in JavaScript −

new DataView(buffer, byteOffset, byteLength)

Here, the buffer is an existing ArrayBuffer used for storage. The byteOffset parameter (optional) represents the offset in bytes to the first byte in the buffer, and the byteLength parameter (also optional) represents the number of elements in the byte array.

Example : Creating a DataView Object

The following example demonstrates how to create a DataView object in JavaScript.

<html>
<body>
<script>
   const buffer = new ArrayBuffer(16);
   //creating dataview object
   const data_view = new DataView(buffer);
   document.write("The type of data_view is: " + typeof(data_view));
</script>
</body>
</html>

Output

The above program displays the type of the data_view as −

The type of data_view is: object

JavaScript DataView Properties

Here is a list of the properties of DataView object −

  • buffer − It returns the ArrayBuffer of SharedArrayBuffer.
  • byteLength − It returns the length (in bytes) of this view.
  • byteOffset − It returns the offset (in bytes) of this view from the start of its ArrayBuffer or SharedArrayBuffer.

JavaScript DataView Methods

Following are the methods of the JavaScript DataView object −

Sr.No. Methods & Description
1

getBigInt64()

It returns a BigInt from the range of -263 to 263-1, included.

2

getBigUint64()

It returns a BigInt from the range of 0 to 264-1, included.

3

getFloat32()

It returns a floating point number from -3.4e38 to 3.4e38.

4

getFloat64()

It returns any number value.

5

getInt16()

It returns an integer from -32768 to 32767, included.

6

getInt32()

It returns an integer from the range of -2147483648 to 2147483647, included.

7

getInt8()

It returns an integer from the range of -128 to 127, included.

8

getUint16()

It returns an integer from the range of 0 to 65535, included.

9

getUint32()

It returns an integer from the range of 0 to 4294967295, included.

10

getUint8()

It returns an integer from the range of 0 to 255, included.

11

setBigInt64()

It returns undefined.

12

setBigUint64()

It returns undefined.

13

setFloat32()

It returns undefined.

14

setFloat64()

It returns undefined.

15

setInt16()

It returns undefined.

16

setInt32()

It returns undefined.

17

setInt8()

It returns undefined.

18

setUint16()

It returns undefined.

19

setUint32()

It returns undefined.

20

setUint8()

It returns undefined.

Advertisements