I am new to JS and I have a question about the return statement. Why do I have to use return false to get out a forEach loop in JS?
This here works fine:
let answer;
let found = false;
for (let i = 0; i < database.length; i++){
let p = database[i];
if (p.id === person.id){
database[i] = person; //Overwrite if found
answer = 'Person information changed.';
found = true;
break;
}
}
But this on the other hand doesn't work:
database.forEach(element => {
if(element.email == req.body.email || element.nachname == req.body.nachname || element.vorname == req.body.vorname)
res.send(element);
break;
});
I got this error -> SyntaxError: Illegal break statement.
Is there any difference between those loops? I know you can break a loop like for, while and switch loop but what makes forEach different?
Thank you!