Transcript from the "Declaring & Assigning Variables" Lesson
[00:01:09] So looking at this line of code, what do y'all notice about it? Just point out some things, what do you notice in this code? And chat, feel free to chime in as well, anything jumping out at us.
>> Purple, [LAUGH] yes, in this particular syntax highlighting theme, something is purple.
[00:01:29] What is purple?
>> Let function.
>> Let, this word let. What is this thing let?
[00:03:12] We're gonna talk a little bit about names in a second. All right, anything else you notice on this line of code or in this line of code? Anything else that may be new that we haven't really seen a lot yet?
>> Well, yes, it's a variable, exactly.
[00:03:32] So the remember is the variable name here, and we're gonna talk about that. What about in terms of the syntax or the symbols that are popping up in this line of code? Is there any character or symbol that is jumping out at us as maybe a little different than something we've seen before, Chu?
>> So we have the equals, the single equals. Yes, so we saw that in a couple of the things that I just told you to write in the computer without explaining them. And the single equals is different than, let's say we just saw the triple equals for the equality operator.
[00:04:36] So excellent point, the equals is doing a lot of heavy lifting here in this tiny line of code. And to the right of the equals, we have "Sept 21", which is something we've seen before. So what is that? Yeah.
>> A string.
>> It's a string, exactly. It is a string value, a literal value.
[00:05:17] And then there's one other thing left that we haven't talked about yet, which is?
[00:05:57] We just unpacked so much stuff out of this one tiny line of code. So let us talk about, Declaring a variable. As Michael Scott from The Office once said, if you just shout, I declare bankruptcy. [LAUGH] Have you declared bankruptcy? I mean, are you now, as far as the government or what have you is concerned, bankrupt?
[00:06:55] And so the question here is, if we go back to our previous example, and I say, let remember = "Sept.21" ; what do you think is the value of the remember variable?
>> A string.
>> A string, which string? Sept.21, yes. Okay, but if I just say let bankruptcy, as in I declare bankruptcy, and then put a semicolon, no equals, no value there, what do you think is the value of bankruptcy now?
>> Any guesses?
>> Why is it undefined? I'm expecting it to be a null.
>> Interesting, so why do we have the, Why do we have the value of a variable that I've just declared, but I haven't given a value yet, why is it undefined and not null? And to this, I would say, let's go back and think about our Backstreet Boys.
[00:09:23] So this is sort of a way that I can say in my program, and usually, if I'm saying this, it's like I'm talking to another coder, maybe he was reading my code later, maybe that coder is me in the future. And I'm saying, I deliberately want this value to be empty, I deliberately want there to be no value here.
[00:10:06] So that is the kind of ain't nothing but a mistake version of nothingness of maybe there is supposed to be something here, I don't know, you didn't tell me, programmer. But I can tell you that there's nothing here, so undefined, I don't have any definition for this. Does that make sense?
[00:10:25] Awesome, great question. Okay, so when we declare a variable like that without giving it a value with let, we can use let to do this, we can later go in and give it a value. And so if just let bankruptcy here is called declaring a variable called bankruptcy.
[00:10:52] In the second line here, what we're doing is assigning a value to that variable that we had declared on the previous line. So what this looks like is if I do, If I do let myDeclaredVariable;, and now I asked for myDeclaredVariable, which now my browser is getting smarter, because it knows that this might be a value that I want, so it has an autocomplete option for me.
[00:11:29] It's undefined, same as the bankruptcy thing. But now, if I type myDeclaredVariable = "so value much wow", Now, if I ask again, what is my declared variable? Now, it has something it remembers at that variable. And so what we've done here is we have essentially split onto two lines, which maybe could be at two different places in our code, the same thing as if I were to say, let remember = some value all on one line.
[00:13:08] And this is also, by the way, what we did before when we said document.query selectorblah.textcontent =. That's also what we did, we assigned a new value to that text content. We'll talk a little bit later about what all those dots were doing. Or we can do it all at once, "such efficient, amaze"; wow.
>> Let bankruptcy?
>> For example.
>> You're just declaring that variable.
[00:16:20] And we're gonna come back to this in a moment to kind of understand what is happening with these variables when we manipulate them. But so I just wanna focus this as a way we can think about the difference between the variable and any values that may or may not be related to the variable.