Open In App

JavaScript DataView Reference

Last Updated : 27 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript dataView is used to control how we can access data, regardless of executing endianness. And provides an interface to read and write more than one number types into an ArrayBuffer.

Syntax:

new DataView(buffer, byteOffset, byteLength)

Example: JavaScript code to show the working of dataView().

Javascript




// Creating an ArrayBuffer with a size in bytes
var buffer = new ArrayBuffer(16);
    
// Creating views
var view1 = new DataView(buffer);
    
// Creating view from byte 0 for the next 4 bytes
var view2 = new DataView(buffer,0,4); 
    
// Creating view from byte 12 for the next 2 bytes
var view3 = new DataView(buffer,12,2);
    
// Putting 1 in slot 0
view1.setInt8(0, 1); 
    
// Putting 2 in slot 12
view1.setInt8(12, 2)
    
// Printing the views
console.log(view2.getInt8(0))
console.log(view3.getInt8(0))


Output:

1
2

The complete list of JavaScript Strings has listed below:

JavaScript DatView Constructor: A constructor gets called when an object is created using the new keyword.

Constructor Description
DatView() Create dataView objects or returns a new DataView object that will represent the specified data buffer.

JavaScript DatView Properties: A JavaScript property is a member of an object that associates a key with a value.

  • Instance Property: An instance property is a property that has a new copy for every new instance of the class.
Instance Properties Description
constructor Return the dataView constructor method for the object.
buffer An ArrayBuffer that is already existing to store the new DataView object.
byteLength Start a new view of the buffer. By default, the new view starts from the first byte.
byteOffset It represents number of elements in the byte array.

JavaScript DatView Methods: Methods are actions that can be performed on objects.

  • Instance Method: If the method is called on an instance of a dataView then it is called an instance method.
Instance Methods Description
getFloat32() Get a 32-bit float at the specified location.
getFloat64() Get a 64-bit float at the specified location.
getInt8() Get an 8-bit integer in the byte at the specified location.
getInt16() Get a 16-bit integer at the specified location.
getInt32() Get a 32-bit integer at the specified location.
getUint8() Get an unsigned 8-bit integer at the specified location.
getUint16() Get an unsigned 16-bit integer at the specified location.
getUint32() Get an unsigned 32-bit integer at the specified location.
setFloat32() Get an assigned 32-bit float value at the specified location.
setFloat64() Get an assigned 64-bit float value at the specified location;.
setInt8() Get an signed 8-bit integer at the specified location.
setInt16() Get an assigned 16-bit integers at the specified location.
setInt32() Get an assigned 32-bit integers at the specified location.
setUint8() Get an unsigned 8-bit integer at the specified location.
setUint16() Get an unsigned 16-bit integer at the specified location.
setUint32() Get an unsigned 32-bit integer at the specified location.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads