DataView.buffer property in JavaScript

The buffer property of the DataView represents the ArrayBuffer of the current DataView. This property provides access to the underlying buffer that the DataView is viewing.

Syntax

dataView.buffer

Return Value

Returns the ArrayBuffer that was used to create the DataView instance.

Example

The following example demonstrates how to access the underlying ArrayBuffer through the buffer property:

<html>
<head>
    <title>JavaScript Example</title>
</head>
<body>
    <script type="text/javascript">
        var arrayBuffer = new ArrayBuffer(156);
        var dataView = new DataView(arrayBuffer);
        
        // Access the buffer property
        document.write("Buffer byte length: " + dataView.buffer.byteLength + "<br>");
        
        // Verify it's the same buffer
        document.write("Same buffer reference: " + (dataView.buffer === arrayBuffer));
    </script>
</body>
</html>

Output

Buffer byte length: 156
Same buffer reference: true

Working with Different DataViews

Multiple DataViews can share the same ArrayBuffer, and their buffer property will reference the same underlying data:

<html>
<head>
    <title>JavaScript Example</title>
</head>
<body>
    <script type="text/javascript">
        var arrayBuffer = new ArrayBuffer(16);
        var dataView1 = new DataView(arrayBuffer, 0, 8);
        var dataView2 = new DataView(arrayBuffer, 8, 8);
        
        document.write("DataView1 buffer length: " + dataView1.buffer.byteLength + "<br>");
        document.write("DataView2 buffer length: " + dataView2.buffer.byteLength + "<br>");
        document.write("Same buffer: " + (dataView1.buffer === dataView2.buffer));
    </script>
</body>
</html>

Output

DataView1 buffer length: 16
DataView2 buffer length: 16
Same buffer: true

Key Points

  • The buffer property is read-only and cannot be modified
  • It always returns the original ArrayBuffer used to create the DataView
  • Multiple DataViews can reference the same ArrayBuffer
  • The buffer property provides access to the entire ArrayBuffer, not just the DataView's portion

Conclusion

The DataView.buffer property provides direct access to the underlying ArrayBuffer, allowing you to work with the raw binary data or create additional views of the same buffer.

Updated on: 2026-03-15T23:18:59+05:30

188 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements