Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Build an Object Challenge, Part 2 Solution

how the JS interpreter/engine works when it comes to for loops

Hello,

One question, I want to make my mind clear about, how the JS interpreter/engine works when it comes to for loops.

I imagine that it enters the loop, checks the info inside parenthesis (parameters). Moves to the code inside the curly braces then finishes the 1st loop.

But how come, it moves to next line of code which was "print" in our example. Prints that to the screen and goes back and continues to 2nd loop? Prints again.. Goes for the 3rd loop.

If this β€œprint” function was inside the loop’s curl braces, i could understand. But this is completely outside, an independent line of code. So in which part of our code, we told the interpreter to read 1 more extra line of code after loop ends. Or how interpreter is not continues reading other lines of code after "print" function. What is stopping it.

I would be glad if someone explains this to me:/ Thank you

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Could you please post the code in question and yes it shouldn't execute code outside the curly brace block until complete. Also, this is a good exercise to go and try it in the console, add some console.log() statements and verify what's happening for yourself :smile:

I did not ask for a spesific code. But I checked it again a few examples, I found what I understood wrong. I was thinking we print the message to the screen. But actually we already printed an empty message at the begining, before the function. var message=' ';

Than we updated the info inside of it. So the for loop was not passing to next line. I thought wrong :) Thank you!

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Sounds good - also watch out for saying thing like "But actually we already printed an empty message at the begining, before the function. var message=' ';" - there's no printing happening, you are declaring a variable called message and assigning it a value of an empty string. Good sign that you are thinking like an interpreter :+1:

1 Answer

Hello, the first javascript is not clearly interpreted. Check here - https://medium.com/dailyjs/understanding-v8s-bytecode-317d46c94775

The next step I suggest you look here at closures - https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20%26%20closures/ch1.md

Also, I suggest you learn C, C++. It helps you understand programming at all. Js engine is based on C++ language. If you want to be really master at it, you need to know a lot of things. Just my suggestion. https://www.programiz.com/c-programming/c-for-loop