Open In App

JavaScript Uint8ClampedArray.of() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The Javascript 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.

Javascript




<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>


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.

Javascript




<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>


Output:

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


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