+ 1

why does it stop after finding the first letter

function ind(word,letter){ for(var i=0;i<word.length;i++){ if(word.slice(i,i+1)==letter){ return "the index is "+i } } } alert(ind("banana","a")) i want to ask why doesnt the loop continue till the end

3rd Oct 2020, 3:50 PM
Ali Zain
Ali Zain - avatar
2 Answers
+ 6
The return statement immediately stops executing the function and returns control back to the caller. That is its purpose.
3rd Oct 2020, 3:53 PM
Brian
Brian - avatar
+ 1
Ohhk thanks Brian
3rd Oct 2020, 4:31 PM
Ali Zain
Ali Zain - avatar