I am trying to write two functions. One contains elements of array with forEach method and another is a callback function that access elements of array from the first function and log their length. The second function is not working and throws Uncaught ReferenceError. I wonder why I get this error.
const fruits = ["Banana", "Mango", "Apple"];
fruits.forEach(listFruits);
function listFruits(allFruits) {
console.log(allFruits);
}
function countFruits(callback) {
callback(allFruits);
console.log(allFruits.length);
}
countFruits(listFruits);
how can i fix this error? and access the length of elements in my callback function , any help will be appreciated
allFruits. It's just the name of an incoming function argument. What do you expect its value to be?callback(allFruits);-> allFruits is not defined in this scope. This is a variable defined only in the scope of listFruits function.callback(fruits.length)