I have a function for a game I am creating:
var getFireableLaser = function() {
var result = null;
lasers.forEach(function(aLaser) {
if(aLaser.y <= -120) {
result = aLaser;
}
});
return(result);
}
it goes through an array of 'laser' objects and returns one when the if condition is met, however if i write the code like this:
var getFireableLaser = function() {
lasers.forEach(function(aLaser) {
if(aLaser.y <= -120) {
return(aLaser);
}
});
return(null);
}
the function only returns null? Why is it that when I do var laser = getFireableLaser(); in another function expression, laser is null when i console.log it? (in that specific other function);
When I console.log(aLaser) just before I return it, it shows the laser object. So why is it that the returned object is null and saved as null?