Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavascriptJavaScript dataView.getInt16() Method

JavaScript dataView.getInt16() Method

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
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments