• JavaScript Video Tutorials

JavaScript DataView setUint32() Method



The JavaScript DataView setUint32() method stores a value as a 32-bit (1 byte=8 bits) unsigned integer in the 4 bytes starting at the specified byte offset of this data view.

It throws a 'RangeError' exception, if the byte offset is outside the bounds of this DataView.

Syntax

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

setUint32(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 32-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, as it only stores the byte value.

Example 1

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

<html>
<body>
<script>
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const value = 20;
   const byteOffset = 0;
   document.write("The data value: ", value);
   document.write("<br>The byteOffset: ", byteOffset);
   document.write("<br>The setUnit32() method returns: ", data_view.setUint32(byteOffset, value));
</script>
</body>
</html>

Output

The above program returns 'undefined'.

The data value: 20
The byteOffset: 0
The setUnit32() method returns: undefined

Example 2

If the byteOffset parameter value is outside the bounds of this DataView, it will throw a 'RangeError' exception.

<html>
<body>
<script>
   const buffer = new ArrayBuffer(16);
   const data_view = new DataView(buffer);
   const value = 24;
   const byteOffset = -1;
   document.write("The data value: ", value);
   document.write("<br>The byteOffset: ", byteOffset);
   try {
      data_view.setUint32(byteOffset, value);
   } catch (error) {
      document.write("<br>Error: " + error);
   }
</script>
</body>
</html>

Output

After executing the above program, it will throw an exception −

The data value: 24
The byteOffset: -1
Error: RangeError: Offset is outside the bounds of the DataView
Advertisements