Home »
MCQs »
JavaScript MCQs
What is the output of the following JavaScript code? | Question 18
68. What will be the output of the following JavaScript code?
<script>
const arr = [10, 20, 30];
let result = 0;
arr.forEach(myFunction);
document.write("Result: " , result)
function myFunction(value, index, array) {
result += value;
}
</script>
- Result: 60
- Result: 102030
- Result: 10,20,30
- ValueError
Answer: A) Result: 60
Explanation:
In the above JavaScript code, we used the forEach() method which is used to call a function (a callback function) once for each array element, and in the callback function, we are adding the elements of the array. Thus, the output would be "Result: 60".