Return usage in for loop

Tell us what’s happening:
i do some search, Methods returning a value.
therefore,why return can’t be used in this case?
if i take return away, the code can be work?
thank you for answer><
Your code so far

// Setup
var myArray = [];
// Only change code below this line
for(var i=1;i<=5;i++){
return myArray.push(i);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36.

Challenge: Iterate with JavaScript For Loops

Link to the challenge:

That will just return on the first iteration of the for loop. It will exit whatever function is there. That push method doesn’t need to return. What do you think the return is doing?

1 Like

the code is not in a function, return is a syntax error outside of a function, breaking code

if it was in a function, you are returning from the first iteration of the loop, meaning the loop only execute for i equal 1 and nothing else

push returns the new length of the array, so what you would be returning in this case is 1, the length of the array myArray, which equals [1]

1 Like

fully understnad !!thank you so much~

thank you for your help :smiley:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.