You’re iterating over every item, so it should print two things as there are two things in the array. If you get rid of the else it will only print the found message if the item is found, otherwise it will do nothing
var toReadData = function(store,id){
for(item of store) {
if (item.productId == id){
return "product id is "+ item.productId;
}
}
return "no item found";
};
Return returns from the function, so breaking the loop. No need for else, just run the loop: if nothing returns, loop is done and it goes onto “no item found”. Use console.log(toReadData(store, 1234)) to log if you want to log
Also just leaving the console logs as-is inside the function is fine, console.log doesn’t do anything