The Javascript TypedArray forEach() method is used to call a specified function once for each element of the array.
Syntax:
array.forEach(function(value, index, array), thisValue)
array.forEach(function(value, index, array), thisValue)
array.forEach(function(value, index, array), thisValue)
Parameters:
value: It represents the current element’s value.
index: It represents the index position of the current element.
array: It represents the array.
thisValue: It represents this argument for the function.
Example 1:
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var Jewels = ["DIAMOND","GOLD","PLATINUM","SILVER"];
Jewels.forEach(function(i)
{ document.write(i+"<br/>")});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var Jewels = ["DIAMOND","GOLD","PLATINUM","SILVER"];
Jewels.forEach(function(i)
{ document.write(i+"<br/>")});
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <script type="text/javascript"> var Jewels = ["DIAMOND","GOLD","PLATINUM","SILVER"]; Jewels.forEach(function(i) { document.write(i+"<br/>")}); </script> </body> </html>
Example 2:
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
function logArrayElements(element, index, array) {
document.write('arr[' + index + '] = ' + element);
document.write("</br>");
}
new Uint8Array([30, 15, 42, 23]).forEach(logArrayElements);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
function logArrayElements(element, index, array) {
document.write('arr[' + index + '] = ' + element);
document.write("</br>");
}
new Uint8Array([30, 15, 42, 23]).forEach(logArrayElements);
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <script type="text/javascript"> function logArrayElements(element, index, array) { document.write('arr[' + index + '] = ' + element); document.write("</br>"); } new Uint8Array([30, 15, 42, 23]).forEach(logArrayElements); </script> </body> </html>