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.

Open Compiler
<html> <body> <script> const buffer = new ArrayBuffer(16); const data_view = new DataView(buffer); const value = 200; const byteOffset = 0; document.write("Value: ", value); document.write("<br>The byte offset: ", byteOffset); //using the setInt32() method document.write("<br>The data_view.setInt32() method returns: ", data_view.setInt32(byteOffset, value)); </script> </body> </html>

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.

Open Compiler
<html> <head> <title>JavaScript DataView setInt32() Method</title> </head> <body> <script> const buffer = new ArrayBuffer(16); const data_view = new DataView(buffer); const value = 21474836478; const byteOffset = 1; document.write("Value: ", value); document.write("<br>The byte offset: ", byteOffset); //using the setInt32() method data_view.setInt32(byteOffset, value); document.write("<br>The store value: ", data_view.getInt32(1)); </script> </body> </html>

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.

Open Compiler
<html> <body> <script> const buffer = new ArrayBuffer(16); const data_view = new DataView(buffer); const value = 243; const byteOffset = -1; document.write("Value: ", value); document.write("<br>The byte offset: ", byteOffset); try { //using the setInt32() method data_view.setInt32(byteOffset, value); } catch (error) { document.write("<br>", error); } </script> </body> </html>

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