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>
  1. Result: 60
  2. Result: 102030
  3. Result: 10,20,30
  4. 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".

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.