What is an expression in JavaScript?
An expression is a fragment of code that produces a value.
For example, you got literal values like 43 or “happy.” These are always expressions!
Expressions are also considered something in between parentheses.
Binary and unary operators that hold 2 expression values or 1 are also fully considered expressions as well.
This also infers that expressions can contain other expressions, nested within each other.
What is a statement in JavaScript?
If you think about expression as part of a sentence, then a statement would be the entire sentence.
A full program consists of a list of statements in JavaScript.
If you’re having trouble recognizing a statement, you can tell by the semicolon in front of it.
If it’s got a semicolon in front of it, that entire line is a statement.
A statement can stand on its own in JavaScript.
Disclaimer: JavaScript sometimes doesn’t require a semicolon at the end of a statement. However, if it’s not required doesn’t mean you have to omit it. You can still just add in a semicolon just to keep it easy to understand.
How to tell the difference between an expression and a statement
Here’s an example of an expression:
22
22 is a literal number.
"happy"
The word happy is also a literal string.
But if you wanted a statement, it looks something like this:
22;
Notice the semicolon to the right of 22? That’s what makes it a statement.
!true;
!true; is a statement because it’s also got a semicolon to the right of it.
Side effects: When a statement changes what comes after it
When a statement is used to change what comes after it, the concept is called side effects.