TypedArray.byteOffset property in JavaScript

The byteOffset property of TypedArray returns the offset (in bytes) from the start of the ArrayBuffer where the typed array begins.

Syntax

typedArray.byteOffset

Parameters

This is a property, not a method, so it takes no parameters.

Return Value

Returns a number representing the byte offset from the beginning of the ArrayBuffer.

Example 1: TypedArray at Buffer Start

<html>
<head>
    <title>JavaScript byteOffset Example</title>
</head>
<body>
    <script type="text/javascript">
        var buffer = new ArrayBuffer(16);
        var float32 = new Float32Array(buffer);
        
        document.write("byteOffset: " + float32.byteOffset);
    </script>
</body>
</html>
byteOffset: 0

Example 2: TypedArray with Custom Offset

<html>
<head>
    <title>JavaScript byteOffset with Offset</title>
</head>
<body>
    <script type="text/javascript">
        var buffer = new ArrayBuffer(24);
        
        // Create TypedArray starting at byte offset 8
        var int16 = new Int16Array(buffer, 8);
        
        document.write("Buffer size: " + buffer.byteLength + " bytes<br>");
        document.write("TypedArray starts at byte offset: " + int16.byteOffset);
    </script>
</body>
</html>
Buffer size: 24 bytes
TypedArray starts at byte offset: 8

Key Points

  • byteOffset is a read-only property
  • Returns 0 when TypedArray starts at the beginning of ArrayBuffer
  • Useful for debugging and understanding memory layout
  • Works with all TypedArray types (Int8Array, Float64Array, etc.)

Conclusion

The byteOffset property helps you determine where a TypedArray starts within its underlying ArrayBuffer. This is essential for memory management and working with shared buffer data.

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

113 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements