Skip to content
Related Articles

Related Articles

JavaScript Uint8ClampedArray.of() Method
  • Last Updated : 14 Sep, 2020

The Uint8ClampedArray array is an array of 8-bit unsigned integers clamped to 0-255. If the value passed is less than 0 or larger than 255 then it will be set instead. If a non-integer is specified, the nearest integer will be set.

The Uint8ClampedArray.of() method creates a new typed array from a variable number of arguments.

Syntax:

Uint8ClampedArray.of(el0, el1, el2, ..., eln)

Parameters:

  • n-elements: This method accepts the number of elements, which are basically the element for which the array is created.

Return Value: This method returns a new Uint8ClampedArray instance.



Example 1: In this example, the values passed are the character values that are converted to Uint8Clamped by the method.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
    <script
        // Creating a Uint8ClampedArray from
        // an array by creating the array from
        // the Uint8ClampedArray.of() method
        let uint8CArr = new Uint8ClampedArray;
        uint8CArr = Uint8ClampedArray.of(
            '40', '51', '56', '18', '24');
        
        // Printing the result 
        console.log(uint8CArr); 
    </script>
</body>
  
</html>

chevron_right


Output:

Uint8ClampedArray(5) [40, 51, 56, 18, 24]

Example 2: In this example, the values passed are the int values that are converted to Uint8Clamped by the method. -9999 and 799 converted to 0 and 255 respectively.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
    <script
        // Creating a Uint8ClampedArray from
        // an array by creating the array from
        // the Uint8ClampedArray.of() method
        let uint8CArr = new Uint8ClampedArray;
  
        // Accepts the uint8C values
        uint8CArr = Uint8ClampedArray.of(
            -9999, 50, 7, 799, 8); 
        
        // Print the result 
        console.log(uint8CArr); 
    </script>
</body>
  
</html>

chevron_right


Output:

 Uint8ClampedArray(5) [0, 50, 7, 255, 8]

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :