The Wayback Machine - http://web.archive.org/web/20211205140639/https://www.geeksforgeeks.org/javascript-uint8clampedarray-from-method/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript | Uint8ClampedArray.from() Method

  • Last Updated : 31 Jan, 2020

The Uint8ClampedArray array represents an array of 8-bit unsigned integers clamped to 0-255. If a value is specified that is not in the range of [0, 255], 0 or 255 will be set instead; if the specified value is not an integer, the nearest integer will be set. By default, the contents of Uint8ClampedArray are initialized to 0. 

The Uint8ClampedArray.from() method is used to create a new Uint8ClampedArray from an array-like or iterable object. So when you want to convert an arrayLike or iterable object to Uint8ClampedArray then you can use this function by passing the object as a parameter to this function along with map function and value used for map function if needed. 

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

Syntax: 

Uint8ClampedArray.from( source, mapFn, thisArg )

Parameters: This method accept three parameters as mentioned above and described below:



  • source: This parameter is an array-like or iterable object which is used to convert to a Uint8ClampedArray object.
  • mapFn: It is an optional parameter which is Map function to call on every element of the Uint8ClampedArray array.
  • thisArg: This parameter is optional which is value to use as this when executing mapFn.

Return Value: This method returns a new Uint8ClampedArray instance. 

Below examples Illustrate the working of Uint8ClampedArray.from() method in JavaScript:

Program 1: 

Javascript




<script>
  
// Create a Uint8ClampedArray from
// a string like structure
var  array = Uint8ClampedArray.from('54323423');
  
// Display the result
document.write(array);
</script>
Output: 
5, 4, 3, 2, 3, 4, 2, 3

Program 2: 

javascript




<script>
  
// Create a Uint8ClampedArray from 
// an array by adding 32 to each
// number using function
var  array = Uint8ClampedArray.from(
    [229, 213, 200, 201, 204], z => z  + 32);
  
// Display the result
document.write(array);
</script>
Output: 
255, 245, 232, 233, 236

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!