JavaScript - DataView
JavaScript - DataView
TheDataViewis an object in JavaScript that allows you to work with the binary data stored in theArrayBuffer. 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.
AnArrayBufferis 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 createDataViewobject, 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, thebufferis an existing ArrayBuffer used for storage. ThebyteOffsetparameter (optional) represents the offset in bytes to the first byte in the buffer, and thebyteLengthparameter (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.
Output
The above program displays the type of the data_view as β
The type of data_view is: objectJavaScript 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 itsArrayBufferorSharedArrayBuffer.
JavaScript DataView Methods
Following are the methods of the JavaScriptDataViewobject β
Sr.No.Methods & Description1getBigInt64()It returns a BigInt from the range of -263to 263-1, included.2getBigUint64()It returns a BigInt from the range of 0 to 264-1, included.3getFloat32()It returns a floating point number from -3.4e38 to 3.4e38.4getFloat64()It returns any number value.5getInt16()It returns an integer from -32768 to 32767, included.6getInt32()It returns an integer from the range of -2147483648 to 2147483647, included.7getInt8()It returns an integer from the range of -128 to 127, included.8getUint16()It returns an integer from the range of 0 to 65535, included.9getUint32()It returns an integer from the range of 0 to 4294967295, included.10getUint8()It returns an integer from the range of 0 to 255, included.11setBigInt64()It returns undefined.12setBigUint64()It returns undefined.13setFloat32()It returns undefined.14setFloat64()It returns undefined.15setInt16()It returns undefined.16setInt32()It returns undefined.17setInt8()It returns undefined.18setUint16()It returns undefined.19setUint32()It returns undefined.20setUint8()It returns undefined.