JavaScript Typed Array Methods

Back to JS page

Table of Contents

The from() Method

TypedArray.from() creates a new typed array from an array-like or iterable object:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Typed Array Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>The from() Method</h4>
<p id="demo"></p>

<script>
// Create Int8Array from a regular array
const regularArray = [10, 20, 30, 40, 50];
const typedArray = Int8Array.from(regularArray);

let text = "Original array: " + regularArray.join(", ") + "<br>";
text += "Typed array: ";
for (let num of typedArray) {
  text += num + " ";
}
text += "<br>Constructor: " + typedArray.constructor.name;

document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Example 1

Result View Example

The of() Method

TypedArray.of() creates a new typed array with a variable number of arguments:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Typed Array Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>The of() Method</h4>
<p id="demo"></p>

<script>
// Create typed array from individual values
const values = Int16Array.of(100, 200, 300, 400, 500);

let text = "Int16Array.of(100, 200, 300, 400, 500):<br>";
for (let num of values) {
  text += num + " ";
}
text += "<br><br>";
text += "Length: " + values.length + "<br>";
text += "Constructor: " + values.constructor.name;

document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Example 2

Result View Example

The fill() Method

fill() fills all elements of a typed array with a static value:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Typed Array Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>The fill() Method</h4>
<p id="demo"></p>

<script>
// Create an array and fill it
const arr = new Int8Array(8);
arr.fill(42);

let text = "Int8Array(8) filled with 42:<br>";
for (let num of arr) {
  text += num + " ";
}
text += "<br><br>";

// Fill with a different value from index 3 to 6
arr.fill(99, 3, 6);
text += "After arr.fill(99, 3, 6):<br>";
for (let num of arr) {
  text += num + " ";
}

document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Example 3

Result View Example

The find() Method

find() returns the first element that satisfies a testing function:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Typed Array Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>The find() Method</h4>
<p id="demo"></p>

<script>
const numbers = new Int8Array([10, 25, 30, 45, 50, 65, 70]);

// Find first element > 40
const found = numbers.find(function(value) {
  return value > 40;
});

// Find first element > 10 and < 30
const found2 = numbers.find(function(value) {
  return value > 10 && value < 30;
});

document.getElementById("demo").innerHTML =
  "Numbers: 10, 25, 30, 45, 50, 65, 70<br><br>" +
  "First > 40: " + found + "<br>" +
  "First > 10 and < 30: " + found2;
</script>

</body>
</html>

Example 4

Result View Example

The some() Method

some() checks if any element satisfies a testing function:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Typed Array Methods</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>The some() Method</h4>
<p id="demo"></p>

<script>
const numbers = new Int8Array([5, 10, 15, 20, 25]);

// Check if any number > 20
const hasLarge = numbers.some(function(value) {
  return value > 20;
});

// Check if any number < 0
const hasNegative = numbers.some(function(value) {
  return value < 0;
});

// Check if any number == 15
const hasFifteen = numbers.some(function(value) {
  return value === 15;
});

document.getElementById("demo").innerHTML =
  "Numbers: 5, 10, 15, 20, 25<br><br>" +
  "Any > 20? " + hasLarge + "<br>" +
  "Any < 0? " + hasNegative + "<br>" +
  "Any == 15? " + hasFifteen;
</script>

</body>
</html>

Example 5

Result View Example

Document

Document in project

You can Download PDF file.

Reference