JavaScript DataView setUint16() Method



The JavaScript DataView setUint16() method is used to store a specified number as a 16-bit (1 byte = 8-bit) unsigned integer in the 2 bytes starting at the specified byte offset of this data view.

If the value of the byteOffset parameter is outside the bounds of this data view or if you haven't passed this parameter to the setUint16() method, it will throw a 'RangeError' exception.

Syntax

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

setUint16(byteOffset, value, littleEndian)

Parameters

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

  • byteOffset − The position in the DataView where the byte will be stored.
  • value − An unsigned 16-bit integer that needs to be stored.
  • littleEndian (optional) − It indicates whether the data is stored in little-endian or big-endian format.

Return value

This method returns undefined.

Example 1

The following program demonstrates the usage of the JavaScript DataView setUnit16() method.






Output

The above program returns the store value as 225.

The data value: 255
The byteOffset: 1
The stored value: 255

Example 2

If the value of the byteOffset parameter is outside the bounds of the DataView, a 'RangeError' exception will be thrown.






Output

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

The data value: 30
The byteOffset: 17
RangeError: Offset is outside the bounds of the DataView

Example 3

If you do not pass the byteOffset parameter to this method, it will throw a 'RangeError' exception.






Output

Once the above program is executed, it will throw a 'RangeError' exception.

The data value: 200
The byteOffset: 1
RangeError: Offset is outside the bounds of the DataView
Advertisements