Why is function returning not defined when it is?

Note. This is just for fun.
I was wondering why my console log statement returns undefined the product is returned right above? I have a quick read through a post on stack overflow but it didn’t make much sense to me https://stackoverflow.com/questions/15694273/console-log-not-printing-variable-from-function
Can somebody please clarify?

function multiplyAll(arr){
  var product =1;
  for (var i = 0; i < arr.length; i++){
    for(var j=0; j < arr[i].length; j++)
      product = product * arr[i][j];
  }
  return  product;
}
//modify values below to test code
multiplyAll([[60,60],[24,365]]);

//why is the product undefined?
console.log(product)

Hey @conicbe,
This is because the variable product has a local scope i.e. it is defined inside the function multiplyAll().
And your console.log()ing it outside of function, and thus you get undefined.

1 Like

Thanks for the reply. :slight_smile: So how would I print the results of the variable product outside the scope of the multiplyAll() function?
I tried calling the function within the console log and defining the product outside of the function but with no luck on either.
Cheers

console.log(multiplyAll([[60,60],[24,365]]))
should do this.