Getting Started with JavaScript, v2

Expressions vs Statements

Kyle Simpson

Kyle Simpson

You Don't Know JS
Getting Started with JavaScript, v2

Check out a free preview of the full Getting Started with JavaScript, v2 course

The "Expressions vs Statements" Lesson is part of the full, Getting Started with JavaScript, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle differentiates between expressions and statements and then counts the number of expressions in an example statement. Kyle later runs an expression and statement to show how they evaluate.

Preview
Close

Transcript from the "Expressions vs Statements" Lesson

[00:00:00]
>> Kyle Simpson: I've used these terms, expressions and statements now several times. Let's make sure we're very clear about what an expression or a statement is. Just like in the English language, an expression is like a phrase, like if you say the sentence, I am a friend who likes to play ping pong.

[00:00:20]
The phrase who likes to play ping pong is not a whole sentence. It's just a part of a sentence. It's a phrase, it's a fragment if you will. In programming speak, we'd called that an expression. And then we take the entire thing from the very beginning to the final period or question mark.

[00:00:36]
And we call that whole thing a sentence, and in programming we call that a statement. A lot of times, there's not a big distinction between expressions and statements. But sometimes it is kind of an important deal to keep them separate. So in line one, we have a statement that begins with this var keyword, which by the way, that's how we tell it, I wanna make a variable.

[00:00:58]
And then it ends like a period in an English sentence. It ends with the semicolon. So that's a whole statement, it's a whole sentence that is saying something that we want to have happen. But there are parts to it and in particular, one of the parts is that age equals 39 part, that's what we call an assignment expression.

[00:01:20]
So we can think of that as a phrase or an expression inside of the whole thing which is the statement. And on line three we see an even more complex sort of statement. Starts with the age at the beginning, ends with the semicolon at the end. Almost all statements will end with a semicolon unless you're dealing with a coding style which likes to leave those off.

[00:01:41]
It'd be kind of like if you were reading Shakespeare and he had no punctuation in it. You might get a little confused where the sentences are. So some people like to do that, but most people like to put the semicolon there. So that's where you know it's at the end of the sentence, it's at the end of the statement.

[00:01:55]
But what are the expressions here? It turns out that there's several, more than you might have realized, in terms of counting things. So let's look at it. The number 2 on the right-hand side, that is an expression. It happens to be called a literal expression. Remember we have those primitive values?

[00:02:11]
Those are called literals, so the number 2 is an expression. And the variable age to the very left of it, that's also an expression, cuz we need to go get whatever's in it, in this case the 39, and do something with it. So we have two expressions already, and then you'll notice those parenthesis.

[00:02:28]
Remember, I was just talking about grouping. Well, we've got a set of parenthesis wrapped around age times 2, times being the multiplication operator, that little asterisk there. That age times 2 with the parenthesis around it, that's another expression, so now we have three expressions. And then there's an expression for the literal 1, so we have a fourth expression.

[00:02:50]
And then we have the expression, 1 plus, and then whatever age times 2 was, and so that's a fifth expression. And then we have the age equals whatever has been computed there, that's a sixth expression. All six of those expressions are part of this one single sentence statement that we call a programming statement.

[00:03:13]
Now again, it's not always that important that you like go and count all of the expressions. But it's at least important that you be aware of the idea that a single statement is often built up with lots of individual expressions. All right, let's try out those expressions and statements, and make sure we see how they're being evaluated.

[00:03:31]
If I run this code, you'll see that it's giving us age 79. If I were to take that specific expression and just ask what was age times 2. Let me comment that out for a minute, if I ran age times 2, you're gonna notice that it was 78.

[00:03:47]
And then that's when we add it to one to get 79. And if I say age times 3, now of course we're gonna get a different age outputted.

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