TypedArray forEach() JavaScript

The Javascript TypedArray forEach() method is used to call a specified function once for each element of the array.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!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>