I'm a very beginner learning Javascript, alongside other code languages, and while looking at information here and in other sites, I've pieced together a simple code loop:
function returnMe() {
for (let i = 1; i <= 10; i ) {
if (i == 10) return i;
console.log(i)
}
}
console.log(returnMe());
I understand what everything does except the function I've created. The function is to stop the loop from continuing endlessly, but I'm not sure how exactly I've enabled that, nor why it should be logged (though I figured out that both pieces must be there for it to work or else it will either not work or come back as undefined.)
I was hoping someone here could help quickly define the simple issue for me, since search engines and other sites believe I'm asking what the return function is instead (which makes sense why), and I just need to understand how that bit works.
CodePudding user response:
the output of the function is 1 2 3 4 5 6 7 8 9 10 it will console log every number from 1 to 9 in the for loop and then it will return 10 which will be printed outside
CodePudding user response:
The For Loop Consists Of for (let i = 1; i <= 10; i )
the parameters: Initiatlisation, Condition, Incrementation. So When You Use i<=10
you are allowing the function to run until the value is less than 10.
Going further It will console log all the numbers from 1 to 10 because of the console.log(i) afterwards. Then at i=10
it will return the value which will be then displayed by console.log(returnMe());