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 Interpreter

When I first started learning about JavaScript, I learned how the JavaScript interpreter works. I learned that it reads your code and runs it, simultaneously, but a few days ago I watched a video from this website that says that the JavaScript interpreter reads through the program. It memorizes the functions and checks for any syntax errors. Then it starts to run the program. I looked into this and found a lot of articles from Stanford and other universities that say that the JavaScript interpreter reads your code and runs it, simultaneously, and I read many articles that say the the JavaScript interpreter reads through your code. It memorizes the functions and checks for any syntax errors. Then it starts to run the program. So, does the JavaScript interpreter read your code and run it, simultaneously, or does it read through the program, memorize the functions, check for any syntax errors, and then it starts to run the program?

1 Answer

Michael Hulet
Michael Hulet
47,913 Points

This probably isn't the answer you're looking for, but this is irrelevant to you as a JavaScript developer, since this is an implementation detail of the particular interpreter you're using, and might vary between implementations. What these articles are likely talking about is that JavaScript is an interpreted language as opposed to a compiled language like C

Both interpreted and compiled languages ultimately need to translate the code the developer writes into code a computer can read an execute. A compiled language splits this translation and the actual running of the code into 2 separate steps. You translate the code to machine-readable code ahead of time when you're done writing it, and you give your users this executable file that they run. With interpreted languages like JavaScript, you just give your users the code you've written, and they run this code directly. This basically combines the translation and run steps into the same step, and this difference is likely what these articles are pointing out

To answer your question more directly, though, is that since JavaScript allows you to reference symbols that are defined later in the file, it's likely that the interpreter you're using at least tokenizes (reads) all of your code first before actually executing any of it

thank you so much for the information Michael Hulet