JavaScript DataView getInt8() Method
The JavaScript DataView getInt8() method reads 1-byte data at the specified byte offset of this DataView and decodes it as an 8-bit signed integer. If you do not specify the byteOffset parameter value to this method, it always returns 0.
This method throws a 'RangeError' exception if the value of the byteOffset parameter is outside the bounds of the data view.
Syntax
Following is the syntax of the JavaScript DataView getInt8() method −
getInt8(byteOffset)
Parameters
This method accepts a parameter named 'byteOffset', which is described below −
- byteOffset − The position in the DataView from which to read the data.
Return value
This method returns a signed integer between the range of -128 to 127, inclusive.
Example 1
The following example demonstrates the usage of the JavaScript DataView getInt8() method.
<html>
<body>
<script>
const buffer = new ArrayBuffer(16);
const data_view = new DataView(buffer);
const value = 127;
const byteOffset = 0;
document.write("Value: ", value);
document.write("<br>The byte offset: ", byteOffset);
//storing the data
data_view.setInt8(byteOffset, value);
//using the getInt8() method
document.write("<br>The stored value: ", data_view.getInt8(byteOffset));
</script>
</body>
</html>
Output
The above program returns the store data value as −
Value: 127
The byte offset: 0
The stored value: 127
Example 2
If we do not pass the byteOffset parameter to this method, it will return 0.
The following is another example of the JavaScript DataView getInt8() method. We use this method to read 1 byte data value 40 at the specified byteOffset 1. Since we do not pass the byteOffset parameter to this method, it will return 0.
<html>
<body>
<script>
const buffer = new ArrayBuffer(8);
const data_view = new DataView(buffer);
const value = 40;
const byteOffset = 1;
document.write("Value: ", value);
document.write("<br>The byte offset: ", byteOffset);
//storing the data
data_view.setInt8(byteOffset, value);
//using the getInt8() method
document.write("<br>The data_view.getInt8() returns: ", data_view.getInt8());
</script>
</body>
</html>
Output
After executing the above program, it will return 0.
Value: 40
The byte offset: 1
The data_view.getInt8() returns: 0
Example 3
If the byteOffset parameter is outside the bounds of this data view, it will throw a 'RangeError' exception.
<html>
<body>
<script>
const buffer = new ArrayBuffer(8);
const data_view = new DataView(buffer);
const value = 121;
const byteOffset = 2;
document.write("Value: ", value);
document.write("<br>The byte offset: ", byteOffset);
//storing the data
data_view.setInt8(byteOffset, value);
try {
//using the getInt8() method
document.write("<br>The data_view.getInt8() returns: ", data_view.getInt8(-1));
} catch (error) {
document.write("<br>", error);
}
</script>
</body>
</html>
Output
Once the above program is executed, it will throw a 'RangeError' exception as −
Value: 121
The byte offset: 2
RangeError: Offset is outside the bounds of the DataView