Expressions Vs. Statements in JS
Statements vs. expressions Knowing this is essential before writing any JS code.
const q = 2
This is a simple line of code that creates a constant q and assigns it the value 5; nothing fancy, right?
const w = hiThere();
It simply creates a const named 'w' and stores whatever value the 'hiThere()' function returns. Now imagine that hiThere() is a massive and extremely complicated function with millions of lines of code that takes 20 years to compute.๐
const q = 2
const w = hiThere();
Now what is the difference between this code, if you think closely and you will know that they are not similar, but obviously you think both doses are the same storing the value in the given const, but it's not right to let me tell you that const q is actually setting a number value and the const w is calling an external function, now we know that this is not the same thing, so well if you would have familiar with functions, arguments, and stuffs like that you cou
These two lines are similar because there is an expression on the right side of the equal sign in both cases.
Expressions? Expression is a piece of code that resolves to a value, in other words, becomes a value, yes 5 is already a value, but for the language interpreter, it's an expression that resolves to the value, for example, we can say that another expression resolve is also example 5+6.
A function call hiThere() is also an expression because this function will return something, so this call will be replaced with the value it returns we can also say the function will be resolved to a value, therefore it's an expression.
not everything in a code becomes a value remember it also not everything in the code is an expression but most things
now let's distinguish between statement and expression
A statement is an instruction, an action for example statements are like loops if, for, and while, and these are namely conditional statements coz they just perform actions and control actions but don't become values, it's very important to understand and see the difference between expressions and statements, seeing the expressions helps you understand the process of computation
10+square(6+4) + square(square(3))
Take a look at this expression, this consists of multiple sub-expressions, lets's break this and see
10
It resolves to 12
square(6+4)
this consists of multiple subexpressions
6+4 resolves 10 and 10 square 100 at this point in the process javascript sees the following that it's not done yet, there are still things to resolve it will continue until the whole expression is resolved to a single value square(square(3)) Resolves like soo, 3 square 9 and 9 square 81, now all the inside expressions are resolved, so now the addition happens in two steps this whole expression is now resolved by the way addition operator has left-associativity, This means that in eh case of multiple additions, the process happens from left to right
You cannot put statements where expressions are expected for example passing a 'const ' statement as a function will produce an error or trying to asign the 'if' statement to a variable just doesn't make any sense in the language, because only expressions are expected in these cases
With this knowledge, you will quickly gain two important superpowers. First, you will be able to see that most of the code, even that million-line-12-year function, is just a collection of things that become values. Second, you will be able to recognise when code simply does not work because it does not make sense in terms of expressions vs statements.
Thank you for reading!
Check out my profiles on these platforms.
Subscribe to my newsletter
Read articles from Karan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Karan
Karan
Hey, I'm Karan! A passionate Fullstack developer from India