JavaScript | typedArray.length() with Examples

The typedArray.length is an inbuilt property in JavaScript which is used to return the length of the given typedArray.
Syntax:

typedArray.length

Parameters: It does not accept any parameter because it is a property not a function.
Return value: It returns the length of the given typedArray.

JavaScript code to show the working of this property:

Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
  // Creating a typedArray with some elements
  var A = new Uint8Array([5, 10, 15, 15, 5, 20, 10]);
  var B = new Uint8Array([5, 10]);
  var C = new Uint8Array([5, 10, 15]);
  var D = new Uint8Array([5, 10, 15, 15, 5]);
  var E = new Uint8Array([5, 10, 15, 15, 5, 20]);
    
  // Calling length() function
  b = A.length     
  c = B.length    
  d = C.length  
  e = D.length
  f = E.length
    
    
  // Printing the length of the typedArray
  document.write(b +"<br>");
  document.write(c +"<br>");
  document.write(d +"<br>");
  document.write(e +"<br>");
  document.write(f +"<br>");
    
</script>

chevron_right


Output:

7
2
3
5
6

Code #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
  // Creating some ArrayBuffer with a size in bytes
  const A = new ArrayBuffer(8);
  const B = new ArrayBuffer(6);
  const C = new ArrayBuffer(16);
  const D = new ArrayBuffer(32);
    
  // Creating some typedArray with the above sizes 
  // of the buffer
  const E = new Uint8Array(A);
  const F = new Uint8Array(B);
  const G = new Uint8Array(C);
  const H = new Uint8Array(D);
    
  // Calling length property
  a = E.length
  b = F.length
  c = G.length
  d = H.length
  
  // Printing the length of the typedArray
  document.write(a +"<br>");
  document.write(b +"<br>");
  document.write(c +"<br>");
  document.write(d);
    
</script>

chevron_right


Output:

8
6
16
32


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.