JavaScript DataView setUint8() Method



TheJavaScript DataView setUint8()method stores an 8-bit unsignedinteger (a byte) at the specified byte offset from the start of the DataView.

If the specified byteOffset is outside the bounds of the DataView, the method throws a'RangeError'exception.

Syntax

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

setUint8(byteOffset, value)

Parameters

This method accepts two parameters named 'byteOffset' and 'value', which are described below −

  • byteOffset − It is the position in the DataView where the byte will be stored.
  • value − An 8-bit unsignedinteger value needs to be stored.

Return value

This method returnsundefined, as it only stores the byte value.

Example 1

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






Output

The above program stores the number and displays it as −

Number value: 20
The byteOffset value: 0
Store value: 20

Example 2

The following is another example of the JavaScript DataViewsetUint8()method. We use this method to store the unsigned 8-bit integer value 50 at the specified byte offset position 2.






Output

Once the above program is executed, it stores the number 50 and displays it as −

Number value: 50
The byteOffset value: 2
Store value: 50

Example 3

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






Output

After executing the above program, it throws a 'RangeError' exception.

Number value: 200
The byteOffset: 9
RangeError: Offset is outside the bounds of the DataView
Advertisements