Confusing for loop

Hey guys,
I’m learning this solution code and don’t understand what stops the for loop from returning more than just the first num making func true (Quote from instructions: “returns the first element in it that passes a ‘truth test’.”).
Won’t the loop keep going till i == arr.length and return every num that answers the condition func(num) == true?

Thanks in advance :slight_smile:

the solution code:

function findElement(arr, func) {
  let num = 0;

  for (var i = 0; i < arr.length; i++) {
    num = arr[i];
    if (func(num)) {
      return num;

  return undefined;

Challenge: Finders Keepers

Link to the challenge:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

a return statement when executed will exit from the function and exit a value - only one return statement can execute in a function

1 Like

Thank you so much! Your’e very helpful :slight_smile: