JavaScript DataView getUint8() Method



The JavaScript DataView getUint8() method is used to retrieve a 1-byte data value at the specified byte offset of this DataView and interprets it as an 8-bit unsigned integer. If no number (value) is found at the specified byteOffset, it will return the number value as 0.

If the byteOffset is outside the bounds of this DataView, invoking the getUint8(-1) method will throw a 'RangeError exception.

Syntax

Following is the syntax of the JavaScript DataView getUint8() method −

getUint8(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 an integer from 0 to 255, inclusive.

Example 1

The following is the basic example of the JavaScript DataView getUint8() method.






Output

The above program read 1 byte and returns 20 −

Number value: 20
The byteOffset value: 0
The data_view.getUint8(0) method returns: 20

Example 2

Ifno numberis found at the specified byteOffset 2, the getUint8() method will return0.






Output

Once the above program is executed, it will return the data value as 0.

Number value: 225
The byteOffset value: 2
The data_view.getUint8(1) method returns: 0

Example 3

If the byteOffset parameter value is outside the bound of the data view, it will throw a 'RangeError' exception.






Output

The program mentioned above throws a 'RangeError' exception after execution.

Number value: 10
The byteOffset value: 0
RangeError: Offset is outside the bounds of the DataView
Advertisements