Check out a free preview of the full JavaScript: The Hard Parts, v2 course

The "Thread of Execution" Lesson is part of the full, JavaScript: The Hard Parts, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Will reviews how JavaScript traverses code with an example while using precise language.

Preview
Close

Transcript from the "Thread of Execution" Lesson

[00:00:00]
>> Will Sentance: Today, we start with principles of JavaScript. JavaScript is not that fancy, it does two things. One, it goes through our code, line by line. There's a block of code we're gonna work through right now. It goes through it line by line, and does each of the line of code, they call this the thread of execution.

[00:00:17]
It threads its way down and executes, that's a fancy word for doing the code, as it goes, that's feature one of JavaScript. Feature two of JavaScript is the stuff that it encounters, if it's saying save something, like a variable, or a constant, or even some code itself, known as a function.

[00:00:36]
Well, it saves that stuff in the computer's memory, to come back to later on and use. All right, and this area here, people, I'm gonna capture, just as JavaScript does under the hood. All of the stuff that's being stored, as we go through the code, line by line.

[00:00:51]
This isn't magic, this isn't me creating an abstraction of what's happening, this is literally what's happening, I mean, not obviously, visually. But this is every line we're storing as we go. And that's all that stuff we encounter is stored into the memory. This is the fancy word in JavaScript, in programming, for the place in which we store data.

[00:01:10]
And so, here we go, who is gonna have the honor of doing line one? Was it Kyla? Kayla, line one, what are we doing with our code here? Verbalize it for me, Kyla.
>> off screen female: You're defining a constant called num.
>> Will Sentance: Beautiful, and?
>> off screen female: Three.
>> Will Sentance: And assigning it the value three.

[00:01:27]
There it is, people, that means, from now on if we see num in our code, we know that it’s gonna go look and find the value, what Kayla?
>> off screen female: 3.
>> Will Sentance: 3, she’s spot on. Excellent to Kayla for being the first person to be called upon. Next line, now I turn to Matt.

[00:01:46]
>> off screen male: We’re defining the function multiply by two.
>> Will Sentance: Thank you Matt, exactly. And this is to say, people, when we define a function, there's two parts. There's our identifier, that is the fancy word for the label for anything you're storing in computer's memory. The identifier here is multiplied by two, the identifier before was num.

[00:02:06]
I recently learned this word, the identifier, and we then take all the code of the function. We're not gonna run any of it right now. We're gonna take it, bundle it up, and store it in the computer's memory, like a string of characters, all the code of that function.

[00:02:20]
I'm not gonna write all that code out, but literally imagine it all saved here. Instead, I'm gonna represent it with this little box with an f in, that represents the function. There you go, all right. And therefore, our thread of execution, the fancy feature of JavaScript, that can take the code, do it, take the code, do it, take the code, do it.

[00:02:40]
Now weaves down to where, Braden. What does it hit next?
>> off screen male: It defines the variable output.
>> Will Sentance: Yeah, I'm gonna call it constant, people tend to seem to be using variable as a generic term for label plus data, identifier plus data. But think about it, it literally means, I can vary the data that I store in that label.

[00:03:04]
And with a constant I can't vary, I can't take away what I had stored there before and store something else there. We'll see more about that later on. So let's say we declare a constant output, excellent. Now Braden, what am I gonna store in output? Do I know yet?

[00:03:20]
>> off screen male: No.
>> Will Sentance: I don't, exactly, because this right hand side is what, Braden?
>> off screen male: It's to your function.
>> Will Sentance: It's-
>> off screen male: A function call.
>> Will Sentance: Function call, excellent from Dan. Excellent from Dan. A call to a function, it's a command. I can only store values, that's fixed, final things.

[00:03:38]
I can't store a go do some work. Dan, what tells me this right hand side is a command?
>> off screen male: The parenthesis.
>> Will Sentance: Parenthesis, exactly, exactly, right. So for now, it's gonna remain uninitialized. This is in contrast to the old days where it's undefined. Because again, if you're declaring a constant output, if we saved the literal data undefined here, well then we wouldn't be able to change it out for the value that comes back from the function.

[00:04:04]
So now we just say it's not really even fully stored yet. I'm gonna show it here, but it's not got any value in it yet. Okay, so we wouldn't even, if we were to inside the running and multiply by two try and log output, we wouldn't see undefined, we'd just see it's not there.

[00:04:18]
Okay, while we head off and run multiply by two. Everybody be ready because this is the core feature of JavaScript. This is the moment that you've all been waiting for. We're gonna call it with num. Kayla, what does num evaluate to? What value does it turn into?
>> off screen female: Three.

[00:04:38]
>> Will Sentance: Three, well done.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now