Transcript from the "Evaluating Code" Lesson
[00:00:27] A few different things are gonna happen, so let's think about it. So one thing that it's gonna do is it's gonna see that let answerToLife, right? And what is it gonna do when it sees let answerToLife?
>> Create the variable.
>> Create a variable, exactly. Let me, Grab a whiteboard here.
[00:00:56] Let's go back to our whiteboard. And I'm just gonna make some space for some new stuff here. Okay, so when we have let answerToLife, we're going to create a new variable. In this case, for brevity's sake, I'll call it answer. Okay, so that's one thing that's gonna happen.
[00:01:48] ((4 + 1) x 2 x 4) etc. That thing is we call a?
>> Evaluates them.
>> Sorry, yes?
>> It evaluates them, right? Or we could say, sorry, did you say something else?
>> It looks at the operations within that expression. It carries out those operations to evaluate the expression. To figure out, given all of these operations, what value does this expression evaluate to once I've performed all of these additions, and multiplications, and whatever. And we can make it quick, this turns out to be 42 [LAUGH] because that's the answer to life, the universe, and everything.
[00:03:00] And then one more thing needs to happen, that's indicated by the equals sign. Yeah, Paul?
>> It assigns that value to the answerToLife.
>> Exactly, so it's gonna create a variable answerToLife. It's going to evaluate the expression, create a value, representing whatever that is, in this case, it's the number 42.
[00:03:25] And then importantly, it's going to create the point, the pointing arrow, which in our minds we can think of as an arrow. It's gonna create a pointer or a reference from the name of that variable to the value of the expression in this case. Okay, so this is review so far, we've talked about this.
[00:04:14] So we have let scrub = guy that thinks he's fly. Also known as a busta, meaning let busta = scrub. And then scrub = 'guy that can't get no love from me'. So these are the things that are happening. So let's walk through and let's think about it together.
>> It's creating a variable called scrub and assigning a value to it.
>> Bingo, okay, so it's creating a variable called scrub. And then when you say assigning a value to it, it's conjuring up, or we're telling it a certain value. Okay, so it's conjuring up a certain value, in this case, it's a string. I'm just gonna abbreviate this as fly [LAUGH] for shorthand.
[00:05:14] Okay, and then you said it's assigning, which again, we said is doing what? How would I draw this in my little whiteboard?
>> With an arrow.
>> An arrow, it's creating a pointer or an arrow in our mental model, but a pointer in computer lingo, I guess, between the name scrub, this new variable I've created, and the value fly.
[00:05:37] Okay, great. Awesome, thank you. How about the next line? Jason, you wanna walk us through this one?
>> So it's creating a new variable called, is that busta?
>> Sorry, it's small. [LAUGH]
>> And then it is assigning, is it creating a pointer to the scrub variable?
>> Great question.
>> Rather than assigning the text to it?
[00:06:44] So it's gonna look at that expression scrub and it's gonna figure out what does that evaluate to? Which means, it's gonna look up in its little table here, in its little contact book, let's say. Look up scrub, and it's gonna say what to scrub evaluate to, which is?
>> Fly, in this case. Guy who thinks he's fly for, yeah, the longer version. And so that is going to then be the value that is assigned to the new variable busta. So the arrow actually doesn't go from scrub to busta, that's not a thing that we can do.
[00:07:58] And it's going to remember that value now as the value of busta. Now where this starts to get interesting is on line three. Well, maybe it's already interesting, hopefully, I think so. But where it starts to actually matter that we have this contact book model in our heads, as opposed to the kind of like box container mental model, is what happens on line three.
[00:08:24] So let's see, Paul, do you wanna maybe talk me through?
>> Okay, so it takes that new string and remembers it as a value because it is assigning that now to scrub.
>> Okay, so it's gonna conjure up some kind of value. I'm gonna represent this as no love, the string, okay?
>> And then it's just going to point scrub to that no love.
>> Exactly, so what it's gonna do is erase the original association, the original pointer between scrub and fly, or guy who thinks he's fly. And instead it's going to create a new pointer, or a new arrow in our mental models, to the string value that I've asked it to remember now.
>> I think it's the new value.
>> Guy that thinks he's fly.
>> That's still fly.
>> So we have two options. Is it gonna be, guy that thinks he's fly? Or is it gonna be, guy that can't get no love?
[00:10:17] It's gonna be guy that can't get no love from me. [LAUGH] Hope you're enjoying these examples. [LAUGH] Busta is actually gonna still be guy that thinks he's fly. This is because, again, that arrow, back to our whiteboard, that arrow that we had drawn from busta, it didn't go, like we said, it didn't go to scrub.
[00:10:48] Busta doesn't actually care what scrub is anymore. Instead, busta only cares what the value of scrub was at the time that it was assigned or, yes, at the time that that was assigned, because that is what the word scrub, the name evaluated to this string value of guy who thinks he's fly.
[00:11:10] And that is what busta in my phone book is now gonna point to. It's sort of like, if I had a phone book and I said, okay, scrub is 555-5555. All right, now busta is a new contact with the same number as scrub, so it's gonna remember 555-5555.
[00:11:28] And now I say, just kidding, scrub changes number to 867-5389. In my contact book, since I kind of copied over the old number for what scrub's number was at the time, that's still gonna be associated to my busta contact. [LAUGH] It's perhaps the most ridiculous example for explaining things, but hopefully it makes the point that this idea that variables are containers that contain a certain value.
[00:12:22] It's going to evaluate the expression on the right of the equals sign, and assign that, create a pointer to the variable that I'm declaring on the left of the equals sign. So that's where that new arrow gets drawn from busta to fly. This one right here. Oops, okay.
[00:12:40] So before we had this, originally. We had an arrow from scrub to fly in line one. We create an arrow to the evaluated value of scrub, which is the string fly. We then, on line three, change the association of scrub to a new thing, but busta has not changed where it points at.
[00:13:28] Keeping in mind that when we assign a variable, we're assigning to whatever the value of the expression on the right-hand side is, is gonna be helpful thing to remember. Question?
>> So I've been deliberately avoiding using the phrases pass by reference and pass by value because it's a little bit trickier than that.
[00:13:55] But when we talk about assigning variables, yes, it's going to assign the variable to the value of that expression at the time, as opposed to a reference to a reference. As opposed to we could theoretically imagine a language in which if I assign busta to scrub, it creates a reference to the pointer, a reference to the reference.
[00:15:11] And the question is, what does the expression that I have assigned this variable to evaluate to at the time that I'm assigning the variable? But however you find it more useful to think about, pass by value, if that's more useful to think about, sure. It also depends a little bit when we're talking about different types of values.
[00:15:30] Right now we've been talking about all strings and numbers and primitive, or we could say sort of simple values. When we talk about arrays and objects, this is gonna get a little bit more complicated. So we'll come back to that thought a little bit later in the course.
[00:15:46] Great question, though.