Open In App

JavaScript dataView.getInt16() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The Javascript dataView.getInt16() is an inbuilt function in dataView that is used to get a 16-bit integer at the specified location i.e, at byte offset from the start of the dataView. The range of 16-bit integer values is from 0 and 65,535 for unsigned and from ?32,768 to 32,767 for signed integer values.
Syntax: 

dataview.getInt16(byteOffset)

Parameters: It has the 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: It returns a 16-bit signed integer value.

Below are examples of the dataView.getInt16() Method. 

Example 1: In this example, we will see the basic use of dataView.getInt16() Method. 

javascript




<script>
    var buffer = new ArrayBuffer(20); 
    var dataview1 = new DataView(buffer, 0, 10); 
    dataview1.setInt16(1, 12); 
    console.log(dataview1.getInt16(1) ); 
</script>


Output: 

12

Example 2: Here as it can be seen that this function does not take float value when this float value is given to this function then it converts that value to integer value. 

javascript




<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);
    console.log(dataview1.getInt16(1));
</script>


Output: 

56 

Example 3: Here as it can be seen that this function does not take float value when this float value is given to this function then it converts that value to integer value. 

javascript




<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 4.5 at slot 1
    dataview1.setInt16(1, 4.5);
    console.log(dataview1.getInt16(1));
</script>


Output: 

4

Example 4: When there is no data to be stored, then it returns NaN i.e, not a number. 

javascript




<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);
       console.log(dataview1.getInt16(1));
</script>


Output:  

0

We have a complete list of Javascript Functions, to check those please go through Javascript Function Complete reference article.
Supported Browsers: 

  • Google Chrome 9 and above
  • Edge 12 and above
  • Firefox 15 and above
  • Internet Explorer 10 and above
  • Opera 12.1 and above
  • Safari 5.1 and above


Last Updated : 22 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads