Learn what an expression is in JavaScript, and how some words make things sound more complicated than they are.
In JavaScript, an expression is simply a way of asking a question that gets answered with a value — and always a single value.
This is an expression:
console.log(5 + 5)
// result: 10
Here’s what’s happening above:
You ask: “JavaScript, what is 5 + 5?”
JavaScript: “5 + 5 evaluates to 10”.
An expression is a way to get the result of or the evaluation of (same thing, different word) of an input — in this case, 5 + 5.
Here’s another expression using the JavaScript typeof
operator:
console.log(typeof([]))
// result: object
In the code above, we ask JavaScript, “what is the type of []
(square brackets)?”
And JavaScript answers “object”.
That’s the essence of JavaScript expressions — you ask and you get an answer (in the shape of a single value).
Surprised? Did you expect this term to evolve something more complicated?
If you did, you’re not alone. Here’s the dictionary definition of expression:
- the action of making known one's thoughts or feelings.
- a look on someone's face that conveys a particular emotion.
I don’t know about you, but there’s nothing in the two definitions above that sounds like it has anything to do with the code example above.
However, if you look at how the term expression is used in math you will probably find it more relatable. But if you’re like me, and your first language is not English, there’s a pretty good chance that you didn’t think about math the first time you heard “expression” in the context of programming.
Final thoughts
When you have a hard time associating words with a context (in this case, programming) it’s usually harder to remember, and that puts up a mental barrier that makes even simple concepts harder to wrap your head around.
This is why I always say that learning the contextual meaning of tech terms is half the battle when you’re learning how to code
Fortunately, the solution is simple. You just need repetition until it sticks. Eventually, you will start to associate expressions with programming when you are in a programming context, just like you associate it with things like “assertion, announcement, looks” and other things outside of programming.