This course has been updated! We now recommend you take the Getting Started with JavaScript, v2 course.
Transcript from the "Statements" Lesson
[00:00:00]
>> Kyle Simpson: A statement, what does that even mean? Well a statement is a coherent set of those words and symbols that go together to express a single idea, a single instruction, if you will. All those statements can definitely be compound just like most of my sentences are quite compound.
[00:00:17] Statements can be compound and they can perform more than one task, but typically there's a roughly one-to-one match between a statement and a concept of what you wanna do. So if I look at this very first statement that we see here, the a = b * 2, what I'll do is I'll try to take these things from the book.
[00:00:36] And I'll try to expound upon them here in my code editor so it's a little bit easier to read. So if we have a = b * 2, this is a single statement. In JavaScript we recognize a single statement most typically by the fact that it ends with this semicolon character.
[00:00:52] Essentially, the semicolon is kinda like a period at the end of an English sentence. It's like, I'm done, I've made that sentence and I'm done, I'm gonna go on to the next sentence. Not all statements require a semicolon. There will be certain forms of statements that won't have an ending semicolon, although you can put them there.
[00:01:09] There's very few places that you could not put a semicolon, but there are places where they're not required. In fact, there's an awful lot of places where they're not technically required, and some people like to leave out semicolons in all of these places and assume. And you can do the same thing in the English language.
[00:01:24] A lot of people do on Twitter. They write entire sentences, and paragraphs worth of content with no punctuation, no spaces, you know, whatever. The hashtag sentences that you've seen where we have no punctuation or spaces. You can do that and probably still get the point across. But I would encourage you as starting out to be a bit more formal about.
[00:01:44] So put your semicolons in the places where it makes sense to end that concept or that task. Here what we're doing is saying I want to do a mathematic operation. So there's an assignment, there's a math statement that's happening. Let's break that down. What does that mean? Well I see the number 2, that's a literal value.
[00:02:02] It doesn't need to be interpreted in any way, shape, or form. We just know it to be a value and its intrinsic value is what we make sense. It is the mathematic number 2. We see that little asterisk symbol to the left of it, and the asterisk symbol is the multiplication operator in most languages, including JavaScript.
[00:02:22] And then to the left of that we see the b variable. Now you notice I'm going in right to left fashion and there's a slight reason why I'm doing that, which is nuanced, but we'll come back to that in a moment. But there's a b operator there, a b character there, and what does that signify?
[00:02:37] Well if it's not otherwise a special recognized word, which we will call those keywords, by the way. Keywords being the special recognized things that a language says. So for example, for, F-O-R, or function, F-U-N-C-T-I-O-N. Those are what we call keywords. And they're words that have special meanings to the language.
[00:03:00] But the b character has no special meaning to this JavaScript language. So if it doesn't have a special meaning but it still looks like a word, then it's a variable. It's called an identifier. And in this case, when an identifier is used in that position as it is, it's representing, it's a symbolic representation of something that holds a value.
[00:03:18] Now in our program we don't see what value it has. So this isn't a complete program, because the b variable would have had to been given some value for this to make any sense. Let's pretend for the moment that the b variable has the value 21 already in it.
[00:03:34] So what this statement says is, go find that b variable, and the rules of how we find that are scope, and we'll come back to those later. But go find that variable and go ask for what its value is and pull that value out. So if we get the value 21 out, multiply that by 2, and we'd get of course the value 42, that special meaning of life value.
[00:03:54] I use that in most of my examples just to be silly. But what we've done is we've computed something on what we'd call the right-hand side. You know, hopefully I don't have to remind you if you're having trouble with left and right, you remember just hold up your left hand and make an L and that's your left, right?
[00:04:11] So, left and right. And I'm sorry, I'm silly because I have a four year old son and a two year old daughter and we're teaching them things like left and right. But on the right-hand side, obviously, there's b * 2. So that b * 2, that's an expression we'll come back to in a moment.
[00:04:24] And then on the left-hand side, we have another variable called a. And in between the two we have an equal statement. And that equal statement is a special kind of operator that means assignment. So essentially what we're doing is we're taking everything on the right-hand side, computing its final value, whatever that might be, and then putting that final value into the thing on the left-hand side.
[00:04:45] You can think about this as right-hand side is the source value, and the left-hand side is the assignment. It's the target value, it's the target location, if you will. So what I'm saying is, compute the value on the right-hand side, which we said would be the value 42, and assign it into the variable a.
[00:05:03] You can see why this, together, it has several different little pieces that we had to parse out, what are these things doing? But as a coherent grouping of things, this is called a statement. And what we've done is compute the value 42, an assignment. So we would actually call this an assignment statement.
[00:05:21] Somebody says in the chatroom, wow the very beginning. Yep, told ya. Very beginning, no experience. If we lose you or if we bore you, just hang on cuz I promise we'll get further into stuff. So that's an assignment statement.