What will be the output of the following JavaScript code? [code] int sum=0; var arr = [10,15,20,30]; arr.forEach(function myFunction(element)

Đăng Khánh

New member
What will be the output of the following JavaScript code?
Code:
int sum=0;
var arr = [10,15,20,30];  
arr.forEach(function myFunction(element) 
{  
    	sum= sum+element;  
});  
document.writeln(sum);
A. 70
B. 75
C. 10
D. error
 
Answer: b
Explanation: forEach is a predefined function in Javascript which used to traverse through the array. It works in a similar way to for loop and iterates through each value in array.
 

Tìm kiếm

Back
Top