JavaScript DataView setInt32() Method



The JavaScript DataView setInt32() method is used to store a number value as a 32-bit signed integer in the 4 bytes, starting at a specified byte offset of this DataView. Multiple bytes can be stored at any offset within the bounds.

If the specified value is not in the range of -2147483648 to 2147483647, it will not store the value in this DataView, and if the byteOffset parameter value is outside the bounds of this DataView, it will throw a 'RangeError' exception.

Syntax

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

setInt32(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 − A signed 32-bit integer that needs to be stored.
  • littleEndian − It indicates whether the data value is stored in little- or big-endian format.

Return value

This method returns 'undefined'.

Example 1

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






Output

The above program returns 'undefined' −

Value: 200
The byte offset: 0
The data_view.setInt32() method returns: undefined

Example 2

If the data value falls outside the range of-2147483648 to 2147483647, thesetInt8()method will not store the specified value as it exceeds the range for an 8-bit signed integer.



JavaScript DataView setInt32() Method





Output

After executing the program, the data value will not be stored since it is outside the range of acceptable values −

Value: 21474836478
The byte offset: 1
The store value: -2

Example 3

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






Output

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

Value: 243
The byte offset: -1
RangeError: Offset is outside the bounds of the DataView
Advertisements