JavaScript: From First Steps to Professional

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 "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 what expressions are and where they can be used in JavaScript. An expression is a valid set of literals, variables, and operators that resolve to a single value.

Preview
Close

Transcript from the "Expressions" Lesson

[00:00:00]
>> Let's move on and talk about another thingy in the JavaScript universe, expressions, because like Madonna, we want to express ourselves in JavaScript. Okay, so expressions, so far, using our operators, we've seen things, we've seen snippets of JavaScript code like this, right, like 4 / 2 * 10 or "Frontend" + "Masters", or etc., etc.

[00:00:30]
Or is 5 greater than 4 not strictly equal to 3 greater than 4? Are these things values? What do y'all think? These snippets of JavaScript, are they values? I'm seeing some nods. I'm seeing some skeptical faces. I can't read faces from the chat. But I'm seeing some quizzical looks, some chin rubs.

[00:00:59]
Hmm, it's kind of a trick question, cuz, again, I'm just tricksy. But are these values? Well, sort of. These are expressions, so all of those things, all of these little snippets of JavaScript using values and operators are expressions. And we wouldn't say that an expression is a value so much as we would say an expression evaluates to a value, or another word you might hear is an expression resolves to a value.

[00:01:31]
Or we could say an expression expresses a certain value, which is where the name comes from. That it's a way of essentially at the end of the day, you are talking about a particular number, a particular truth value, for example, a particular Boolean value. But the expression has more kind of content to it, more information.

[00:01:52]
And we have expressions in natural language too, right? Like the amazing awesome twins movie [LAUGH] New York Minute, the expression, a New York minute, in English, in American English, at least, let's say. Have folks heard it, have folks heard the expression like, be there in a New York minute?

[00:02:12]
Not a little bit? Yes, okay, so Jason, what is a New York minute?
>> My understanding of it is it's a very quick minute, very fast.
>> Very fast, exactly. So we say because things in New York City moves so fast and everybody walks so fast and things happen all the time.

[00:02:30]
[LAUGH] Because things are happening so quickly, this expression in English, in American English, a New York minute means a second, or an instant, or some very short unit of time. So this-three word expression, New York minute, really means the value of an instant, like I'll be there in an instant, I'll be there in a New York minute.

[00:02:54]
So we have expressions in natural language too. And similarly, JavaScript expressions, they equate to a particular value, but there's kind of more going on. So when we're, for example, multiplying and dividing and things like that, that is a longer expression which evaluates to a particular value. And again, expressions are all covered in detail on MDN, there's a whole guide to learning more about expressions.

[00:03:23]
But suffice it to say that these things that we've been working with, these chunks of JavaScript are what we call expressions, and they can go in places where a value would be expected. So for example, we can pass them in to maybe one of our spelf, like index of, for example.

[00:03:52]
Or when we're asking, does a string include the string Frontend Masters, instead of an individual string, we can pass in a expression that evaluates to a string. So for example, I could say, FrontendMasters is a string. If I ask it does it include "Front" + "end"? And it's going to say true, because what JavaScript sort of sees in here is the value of this "Front" + "end" thing expression, I can now say, instead of thing or stuff.

[00:04:43]
This expression evaluates to the value Frontend, which then JavaScript can answer whether the string Frontend Masters includes that string value. So this is all to say expressions are, they function in the JavaScript universe as values, and we can use them in places where we would use a value.

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