This course has been updated! We now recommend you take the Getting Started with JavaScript, v2 course.

Check out a free preview of the full Introduction to JavaScript Programming course:
The "Executing a Program" Lesson is part of the full, Introduction to JavaScript Programming course featured in this preview video. Here's what you'd learn in this lesson:

Statements and expressions make it easy for developers to tell the program what to do. When it comes time for the computer to execute the code, it needs to be either interpreted or compiled into a language the computer will understand.

Get Unlimited Access Now

Transcript from the "Executing a Program" Lesson

[00:00:00]
>> Kyle Simpson: Okay so let's go back to the text, I know I kinda jumped around a little bit and I'll well let's a get ourself back on track and make sure we understand that we track back with the text. So, [COUGH] and the next topic that's addressed is what we've only talked about so far, is how to think about what a program is structured in.

[00:00:23] And maybe start to thinking our brains about what we do if we wanted to do it, but it turns out that it's much more important for the computer to run our program, in the end than it is for us to run our program. So, it is that duality as was brought up before, between needing to be able to look at a program and understand what it should do, and then being able to rely upon the computer to do what it actually must do.

[00:00:45] And those are two really important halves of the brain, if you will. So executing a program, what does that mean? Well, essentially it means, turning the statements that we've written, like a equals b times 2, into low level instructions, that a computer can do something with. The computer itself would look at a equals b times 2 and have no idea what that meant.

[00:01:07] JavaScript knows what it means. JavaScript needs to translate it into something else, that then can be translated and translated further and further down the stack, until eventually, you get to that series of 1s and 0s and the computer says, I know what to do with that. The 1 means put an electrical charge in and the 0 means turn it off, and I can switch back and forth between 1's and 0s really fast, and run all those things through the circuitry in my computers CPU, and out pops a number, right?

[00:01:33] So, it's this process of getting something from this high level language, like JavaScript, this a equals b times 2, which isn't exactly like natural language. It's not like I said it'd be interesting if there was a programming language where I could say, set the variable a to the value 2, right?

[00:01:55]
>> Speaker 2: Cool.
>> Kyle Simpson: There are languages that are even higher level, than JavaScript is. And so understanding that sort of, what we call it in programming we call it abstraction. At this level, it looks like an English sentence. There would have to be a translation from that into something lower level cuz the computer would look at that and equally be confused.

[00:02:19] So a translation level would go from that to maybe something like a = 2, right. And then something would have to go from a = 2 down into another layer, and we might be getting too far field, but I just wanna make sure you understand. So the next layer down might be something that people refer to as assembly language, which is still a high level language, it's not a 1 and a 0 but it's lower level.

[00:02:44] And I don't even memorize assembly language but it's something like mov a,2 or something like that or I think it's 2,a or something like that, but that lower level thing is like, I got these two different registers in memory or whatever and move one into the other, right?

[00:03:02] And then finally, at the lowest level of the stack its taken that thing down into a series of 1s and 0s. That's really, really long and that 1 in 0 string tells the computer what to do. So what we want, is we wanna figure out to execute my program of course we don't need this top level, but we definitely need this a = b level.

[00:03:22] We need something to convert it from that layer down into the next layer. There'll be something else that handles it on and on and on, however many steps it needs to take. But we need something to do that first step, and that something is the JavaScript engine, the thing that's built into our browser, the thing that you can invoke in the developer console.

[00:03:40] If you were doing programming on a server, there's an environment where JavaScript can run on a server. And I mean there's all kinds of different places. JavaScript can run on watches and inside of light bulbs, and whatever. But in every place that there is, there's a JavaScript engine, that takes something like a = 2 and turns it into something that that environment knows what to do with.

[00:04:00] And that's what we mean by executing a program. It's taking that source code that we've written in text form, converting it into something that the next layer down knows what to do with, okay? Now, quick little kind of myth busting thing. It is often said of languages like JavaScript, that they are scripting languages.

[00:04:21] Of course the word script shows up in JavaScript, so it's natural to assume, that it's a scripting language. And it's also often said that scripting languages are interpreted. In other words, one statement at a time, it works on that statement and then it goes to the next statement and then the next statement.

[00:04:40] So when it's working on the statement on line three, it has no idea what to expect of line four cuz it hasn't gotten there yet. It's like if you're working through a to-do list, and you're like I don't even care about item two yet, cuz the top item on my to-do list is the only thing I'm thinking about, right?

[00:04:55] Well, that's what we mean by interpreting, and there are languages that are, in fact, interpreted like that, that are interpreted line by line by line. An example of that might be something like a command line scripting like Bash or whatever. It's not fully interpreted, there's a little bit of nuance there but it's essentially interpreted line by line.

[00:05:13] And it's often times assumed that JavaScript is the same way. It's often times assumed that JavaScript interpreted with line one and then line two and then line three, there have been many books that have said that. And it is true that in the very earliest days of JavaScript it worked a lot more like that.

[00:05:29] It's definitely true that when Brandon was first inventing it as a prototype, he didn't really have all the time to go through all of these sophisticated stuff so he had to make just bank something out quickly, and it cut a lot of corners. And so essentially, in the very beginning it was kind of that way.

[00:05:44] But very quickly, JavaScript matured, and by very quickly, I mean within those first couple of years, JavaScript matured to the point where interpreting it wasn't really practical, anymore. And yet, that myth propagates even today, people say, JavaScript's a scripting language. It's interpreted, it's not. And the other option, if it's not an interpreted language, the other thing that we talk about is what's called a complied language.

[00:06:10] And what a compiled language and an interpretative language, the way that they primarily differ, is not the way you might think that they differ. So most people think of compilation as I'm producing some physical series of 1s and 0s and putting those into a file, that I can send that series of 1s and 0s to other people, and they don't need to process it, they just need to run it.

[00:06:31] And they think about interpreted languages as not doing that, that every time you wanna run it, it has to reprocess from the beginning. So that's one way of thinking about the difference between interpreting and compiled but the more important distinction, the one that matters to JavaScript, is that compilations means, that there is more than one pass through your program.

[00:06:55] So when you put a program like this, if that was in a file and you loaded that up into browser, line one would never run, you'd immediately get an error saying line two is invalid. Whereas if you put this into a scripting language like a bash or something, line one would run and then you get an error on line two.

[00:07:10] And that's the major distinction that we wanna make between this compilation versus interpreting. The good news is that because JavaScript's compiled, a lot of these mistakes that you can make in your programming, are gonna get caught at the very beginning, before you've had a chance to mess something up.

[00:07:27] You try to run your program and the first thing that happens is it gets compiled. And if the compilation throws an error, and says, hey, you've messed up, that's not good. Then you know what you need to fix, it'll tell you, line two is invalid, two's not a function, or whatever.

[00:07:43]
>> Kyle Simpson: Okay, so that's the distinction we'll make, and, we'll kind of leave it there. There's definitely more details, and I expound on that a lot more in the scope and closures book in this series. But we'll kind of leave it for there, think of JavaScript more like a compiled language than an interpreted language.

[00:08:00] And that is, it doesn't really matter that JavaScript doesn't get put into binary form and distributed the way the exe files are. What matters is that JavaScript gets compiled for error checking.