Why does everything in the code not get executed?

Here is a function from the Exercise 2: Includes of the Arrays sub-course in Mosh’s javascript course 1. I need to know why when the if statement is true and we jump out of the loop. The next line doesn’t get executed but if the if statement is false, the next line is executed


function includes(array, searchElement) {
    for (let element of array)
        if (element === searchElement) return true;

*// Why does this next line not get executed if the 'if' statement above is true?*

        return false;

Because return leaves the function and returns to the caller.

1 Like

Oh, I never knew that return statements stopped the execution of a function. Thank you for letting me know. :slight_smile: