JavaScript: From First Steps to Professional

Statements vs Expressions

Anjana Vakil

Anjana Vakil

Software Engineer & Educator
JavaScript: From First Steps to Professional

Check out a free preview of the full JavaScript: From First Steps to Professional course

The "Statements vs Expressions" Lesson is part of the full, JavaScript: From First Steps to Professional course featured in this preview video. Here's what you'd learn in this lesson:

Anjana discusses the differences between statements and expressions and provides a brief overview of the material covered. An expression asks JavaScript for a value, while a statement tells JavaScript to do something.

Preview
Close

Transcript from the "Statements vs Expressions" Lesson

[00:00:00]
>> So we talked about expressions earlier. We know that express values like New York Minute means a second for instance. And we also have something in JavaScript called statements that are kind of. It's not something that you're going to need to necessarily know and think about all the time.

[00:00:18]
But it is a little bit useful to talk about the difference between statements and expressions. And as an actor, I enjoy Colman Domingo says in this case, I can't tell if it's a statement or a question. So, expressions, we could sort of think about as being like, questions, we asked JavaScript that say, hey, what is this value?

[00:00:38]
So we could say that an expression asks JavaScript for a particular value. Like my assigned variable says, hey, JavaScript, what's the current value of my assigned variable? Or hey, JavaScript, what is 6 plus 4? That is the value 10. Or, hey, JavaScript, what is this element with a certain ID?

[00:00:56]
And that is a HTML element. So, an expression as we said, it sort of evaluates to a value. Another way we could think about that is it's kind of a question that we asked JavaScript, what is this value? A statement is something we tell JavaScript to do. So, for example, we've seen statements already that declare or maybe just assign, or declare and or assign values to variables.

[00:01:26]
So for example, let 10 equals 6 plus 4; is a statement. It says, hey JavaScript, do a few things, create a variable called evaluate the expression 6 plus 4, and assign the value of that expression to the variable 10. JavaScript, do all of those things. So, we are telling JavaScript to do stuff.

[00:01:50]
Whereas let's say the expression 6 plus 4 is asking JavaScript, hey, JavaScript, what is the number that is equal to 6 plus 4? And so on and so forth. If we are, maybe we've already declared a variable, even just declaring a variable is a statement. And then assigning a variable to a new value if we've declared it with let, is also a statement.

[00:02:12]
So these are examples of statements. And we notice that these statement, they tend to have a semicolon at the end of them, like a punctuation to say JavaScript, I am done telling you stuff to do for now [LAUGH]. And so this is gonna be kind of something that we're gonna see more of.

[00:02:35]
We're going to see more of throughout the course. We're gonna see other types of statements that are telling JavaScript to do different things. For example, tomorrow we're going to talk about functions. Functions have a return statement usually, that it's going to tell JavaScript, hey, give me back this value from this function.

[00:02:52]
We're going to talk more about it tomorrow. We also have things like conditionals that are going to have more complexity around, if something is true, then do some other thing, and if something is false, then do something else. So, we also have things like for statements in JavaScript, which are gonna be loops.

[00:03:18]
And we also saw the statement console log earlier. That is another statement of telling JavaScript hey, JavaScript, print this thing to the console. So suffice it to say that there is kind of in our mental models here, there's a distinction we can draw between expressions which can be represented as values, right?

[00:03:37]
They are equivalent to values and statements, which are more like actions that we want JavaScript to take, to do stuff. So, I bring this up just to say that when we're thinking about our JavaScript programs, and the chunks of code inside of them. Sometimes, we're gonna need a statement, a command for Java Script to do something, and sometimes we're gonna need an expression.

[00:04:06]
So basically, a long winded way of asking JavaScript for a particular value. And they're not always interchangeable. So, as we get used to the syntax of JavaScript, start wrapping our heads around when we need a statement in a certain place, and when we need an expression in a certain place.

[00:04:27]
So, this is vocabulary, is not so much important, so much as for us to keep in mind that sometimes we're asking JavaScript to essentially conjure a value. And other times we're asking JavaScript to do stuff, create a new variable or log something to the console or return a value.

[00:04:51]
And that is, if you find in the literature and blog posts, in whatever the words statement and expression, that is the meaning of these words. And of course, guess where you can read more about statements and expression? MDN. Yes, you get it by now, okay. So at this point, we're going to move on and start talking about collection of things like arrays and objects, as I said.

[00:05:19]
And our friend Michelle Yeoh is going to help us do that. But in the interests of not overloading our brains and making them explode from too much JavaScripty goodness. And also because we're humans and we get tired and we are not the machines on which we run our JavaScript code, we are humans with needs for food and sleep and things like that, pesky little needs that we have.

[00:05:42]
We are going to take a break here and come back to this, start this up in part two, day two, and jump in to arrays and objects, and finish our Tic-Tac-Toe game tomorrow. So before we do that, I'm just zooming out and recapping what we've done so far.

[00:06:01]
We talked about statements and expressions. We learned about expressions as ways to express the value. We talked about variables and all of the different ways we have to declare and assign them. And we developed a strong mental model for How JavaScript actually performs that task of declaring variables and figuring out what values to point them to.

[00:06:29]
And that's gonna help us out a lot as we continue in our journey etc. Glad you'll remember us. And we also talked about lots of different values and data types. And some of the operators that we can use on particular data types like strings, and like our good friends, numbers, booleans, undefined and null.

[00:06:56]
So we've really covered a lot of ground. And this is all on top of the fact that we actually made some changes to our document. Thanks to the Document Object Model. We've gone through all kinds of stuff. We've manipulated web pages. We've talked about different types of values.

[00:07:12]
We've figured out how to express values with more complex expression. We figured out how to remember those values as variables. We're going to dig into even more complex types of variables, namely arrays and objects. Before looking at functions and other ways that we can get JavaScript to remember stuff we want to do with our chunks of code.

[00:07:32]
Thank you all so much. For all of your great questions and example code, and all of that, exploration of some of the other things we've heard about in JavaScript which we can always look up on our friend MDN.

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