• JavaScript Video Tutorials

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.

<html>
<body>
<script>
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const value = 255;
   const byteOffset = 1;
   document.write("The data value: ", value);
   document.write("<br>The byteOffset: ", byteOffset);
   //using the setUnit16() method
   data_view.setUint16(byteOffset, value);
   document.write("<br>The stored value: ", data_view.getUint16(1));
</script>
</body>
</html>

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.

<html>
<body>
<script>
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const value = 30;
   const byteOffset = 17;
   document.write("The data value: ", value);
   document.write("<br>The byteOffset: ", byteOffset);
   //using the setUnit16() method
   try {
      data_view.setUint16(byteOffset, value);
      document.write("<br>The stored value: ", data_view.getUint16(1));
   }catch (error) {
      document.write("<br>", error);
   }
</script>
</body>
</html>

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.

<html>
<body>
<script>
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const value = 200;
   const byteOffset = 1;
   document.write("The data value: ", value);
   document.write("<br>The byteOffset: ", byteOffset);
   //using the setUnit16() method
   try {
      //not passing byteOffset parameter
      data_view.setUint16(value);
   }catch (error) {
      document.write("<br>", error);
   }
</script>
</body>
</html>

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