Skip to content
Related Articles

Related Articles

Improve Article

JavaScript dataView.setInt16() Method

  • Last Updated : 16 Sep, 2020
Geek Week

Below is the example of the dataView.setInt16() Method.

  • Example:




    <script>
       var buffer = new ArrayBuffer(20);
      
       var dataview1 = new DataView(buffer, 0, 10);
       dataview1.setInt16(1, 12);
       document.write(dataview1.getInt8(1));
    </script>
  • Output:
    12

The dataView.setInt16() is an inbuilt function in dataView which is used to store an signed 16-bit integer at the specified location i.e at byte offset from the start of the dataView.

Syntax:

dataView.setInt16(byteOffset)

Parameters: It has parameter byteOffset which is offset in a byte and it says where to read the data from the beginning(start) of the view.

Return value: This function does not return anything.



Example 1:

Input: dataview1.setInt16(1, 56); 
Output: 56

Example 2:

Input: dataview1.setInt16(1, Math.PI); 
Output: 3

JavaScript code to show the working of this method
Code #1:




<script>
  
   // Creating buffer with size in byte
   var buffer = new ArrayBuffer(20);
  
   // Creating a view
   var dataview1 = new DataView(buffer, 0, 10);
  
   // put the data 56 at slot 1
   dataview1.setInt16(1, 56);
   document.write(dataview1.getInt16(1));
     
</script>

Output:

56

Code #2:
This function does not accept float value that is why it converts float value to integer value. It can be seen from the output of the below program that program should give output as 3.14 (the value of PI) but this function convert this value to 3.




<script>
  
   // Creating buffer with size in byte
   var buffer = new ArrayBuffer(20);
  
   // Creating a view with slot from o to 10
   var dataview1 = new DataView(buffer, 0, 10);
  
   // put the value of PI at slot 1
   dataview1.setInt16(1, Math.PI);
   document.write(dataview1.getInt16(1) + "<br>");
     
</script>

Output:

3

Code #3:
When there is no data to be stored, then it returns zero (0).




<script>
  
   // Creating buffer with size in byte
   var buffer = new ArrayBuffer(20);
  
   // Creating a view
   var dataview1 = new DataView(buffer, 0, 10);
  
   // putting no data at slot 1
   dataview1.setInt16(1);
   document.write(dataview1.getInt16(1) + "<br>");
     
</script>

Output:

0

Supported Browsers:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Apple Safari
  • Opera

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!




My Personal Notes arrow_drop_up
Recommended Articles
Page :