HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <h2>Lesson notes</h2>
1 <h2>Lesson notes</h2>
2 <p>An expression is any valid unit of code that resolves to a value (<a>source</a>).</p>
2 <p>An expression is any valid unit of code that resolves to a value (<a>source</a>).</p>
3 <p>Below 5 is an expression, it resolves to value 5:</p>
3 <p>Below 5 is an expression, it resolves to value 5:</p>
4 <p>Below getAnswer() - a function call - is another expression. This call will return a value, i.e. this function call will resolve to a value:</p>
4 <p>Below getAnswer() - a function call - is another expression. This call will return a value, i.e. this function call will resolve to a value:</p>
5 <p>Below is an example of an expression that consists of multiple<em>subexpressions</em>, and the step-by-step process of resolving each expression in order until the whole expression is resolved to a single value:</p>
5 <p>Below is an example of an expression that consists of multiple<em>subexpressions</em>, and the step-by-step process of resolving each expression in order until the whole expression is resolved to a single value:</p>
6 <p>JavaScript distinguishes<strong>expressions</strong>and<strong>statements</strong>. A statement is (roughly) an instruction, an action.</p>
6 <p>JavaScript distinguishes<strong>expressions</strong>and<strong>statements</strong>. A statement is (roughly) an instruction, an action.</p>
7 <p>if, while, for, const are examples of statements. They perform actions or control actions, but don't resolve to values.</p>
7 <p>if, while, for, const are examples of statements. They perform actions or control actions, but don't resolve to values.</p>
8 <h2>Optional reading</h2>
8 <h2>Optional reading</h2>
9 <ul><li><a>Expressions versus statements in JavaScript</a></li>
9 <ul><li><a>Expressions versus statements in JavaScript</a></li>
10 <li><a>Operator precedence in JavaScript</a></li>
10 <li><a>Operator precedence in JavaScript</a></li>
11 <li><a>Expressions / MDN</a></li>
11 <li><a>Expressions / MDN</a></li>
12 </ul><h2>Lesson transcript</h2>
12 </ul><h2>Lesson transcript</h2>
13 <p>Look at this simple line of code:</p>
13 <p>Look at this simple line of code:</p>
14 <p>You know exactly what's going on, right? Create a new constant named x, put the value 5 in it. Not much to it.</p>
14 <p>You know exactly what's going on, right? Create a new constant named x, put the value 5 in it. Not much to it.</p>
15 <p>Here is another line of code:</p>
15 <p>Here is another line of code:</p>
16 <p>Create a new constant named y, put inside whatever value getAnswer function returns. Now, imagine this getAnswer is actually a huge and extremely complicated function with millions of lines of code, and it takes 12 years to compute.</p>
16 <p>Create a new constant named y, put inside whatever value getAnswer function returns. Now, imagine this getAnswer is actually a huge and extremely complicated function with millions of lines of code, and it takes 12 years to compute.</p>
17 <p>How different are these lines of code? Well, it turns out that in computer science a much more powerful question is usually "how similar are these?".</p>
17 <p>How different are these lines of code? Well, it turns out that in computer science a much more powerful question is usually "how similar are these?".</p>
18 <p>And, the answer is, of course, "it depends".</p>
18 <p>And, the answer is, of course, "it depends".</p>
19 <p>If you think in terms of what is actually, literally going on - they are not similar at all. One is setting a number value, another one is calling an external function. We know very well this is not the same thing. We know about functions, and arguments, and all that stuff.</p>
19 <p>If you think in terms of what is actually, literally going on - they are not similar at all. One is setting a number value, another one is calling an external function. We know very well this is not the same thing. We know about functions, and arguments, and all that stuff.</p>
20 <p>But sometimes it's useful to think in other terms, on another level. Sure, running and flying a plane are very different activities, but on a certain level they are similar - both are about moving from point to point.</p>
20 <p>But sometimes it's useful to think in other terms, on another level. Sure, running and flying a plane are very different activities, but on a certain level they are similar - both are about moving from point to point.</p>
21 <p>These two lines are similar because on the right side of the equal sign there is an expression in both cases. Expression is a piece of code that resolves to a value. In other words, becomes a value. I know, 5 is already a value, but for the language interpreter it's an expression that resolves to the value 5. Another expression that resolves to the value 5 is 2 + 3, for example.</p>
21 <p>These two lines are similar because on the right side of the equal sign there is an expression in both cases. Expression is a piece of code that resolves to a value. In other words, becomes a value. I know, 5 is already a value, but for the language interpreter it's an expression that resolves to the value 5. Another expression that resolves to the value 5 is 2 + 3, for example.</p>
22 <p>A function call getAnswer() is also an expression, because this function will return something. So, this call will be replaced with the value it returns. In other words, the function call will be resolved to a value, therefore it's an expression.</p>
22 <p>A function call getAnswer() is also an expression, because this function will return something. So, this call will be replaced with the value it returns. In other words, the function call will be resolved to a value, therefore it's an expression.</p>
23 <p>Not everything in the code becomes a value. So, not everything in the code is an expression, although, most things are.</p>
23 <p>Not everything in the code becomes a value. So, not everything in the code is an expression, although, most things are.</p>
24 <p>JavaScript distinguishes expressions and statements. A statement is an instruction, an action. Remember conditions with if, loops with while and for - all those are statements, because they just perform actions and control actions, but don't become values.</p>
24 <p>JavaScript distinguishes expressions and statements. A statement is an instruction, an action. Remember conditions with if, loops with while and for - all those are statements, because they just perform actions and control actions, but don't become values.</p>
25 <p>Is this one of those obscure textbook technicalities? Might seem that way, but it's actually very important to understand and see the difference between expressions and statements.</p>
25 <p>Is this one of those obscure textbook technicalities? Might seem that way, but it's actually very important to understand and see the difference between expressions and statements.</p>
26 <p>Seeing expressions helps you understand the process of computation. Take a look at an example:</p>
26 <p>Seeing expressions helps you understand the process of computation. Take a look at an example:</p>
27 <p>This expression consists of multiple subexpressions.</p>
27 <p>This expression consists of multiple subexpressions.</p>
28 <p>First one - 12 - resolves to 12. Next one consists of multiple subexpressions:</p>
28 <p>First one - 12 - resolves to 12. Next one consists of multiple subexpressions:</p>
29 <ul><li>7 resolves to 7</li>
29 <ul><li>7 resolves to 7</li>
30 <li>5 resolves to 5</li>
30 <li>5 resolves to 5</li>
31 <li>7 + 5 resolves to 12</li>
31 <li>7 + 5 resolves to 12</li>
32 <li>square(12) resolves to 144</li>
32 <li>square(12) resolves to 144</li>
33 </ul><p>At this point in the process JavaScript sees the following picture:</p>
33 </ul><p>At this point in the process JavaScript sees the following picture:</p>
34 <p>It's not done yet, there are still things to resolve. It will continue until the whole expression is resolved to a single value.</p>
34 <p>It's not done yet, there are still things to resolve. It will continue until the whole expression is resolved to a single value.</p>
35 <p>square(square(2)) resolves like so:</p>
35 <p>square(square(2)) resolves like so:</p>
36 <ul><li>2 resolves to 2</li>
36 <ul><li>2 resolves to 2</li>
37 <li>square(2) resolves to 4</li>
37 <li>square(2) resolves to 4</li>
38 <li>square(4) resolves to 16</li>
38 <li>square(4) resolves to 16</li>
39 </ul><p>Let's look inside the interpreter's mind again:</p>
39 </ul><p>Let's look inside the interpreter's mind again:</p>
40 <p>All the inside expressions are resolved, so now the addition happens in two steps:</p>
40 <p>All the inside expressions are resolved, so now the addition happens in two steps:</p>
41 <p>This whole expression is now resolved.</p>
41 <p>This whole expression is now resolved.</p>
42 <p>By the way, addition operator has left-associativity. This means that in the case of multiple additions, the process happens from left to right, that's why we first see 12 + 144 and then 156 + 16.</p>
42 <p>By the way, addition operator has left-associativity. This means that in the case of multiple additions, the process happens from left to right, that's why we first see 12 + 144 and then 156 + 16.</p>
43 <p>You can't put statements where expressions are expected. For example, passing a const statement as a function argument will produce an error. Or trying to assign the if statement to a variable. This just doesn't make sense in the language, because only expressions are expected in these cases:</p>
43 <p>You can't put statements where expressions are expected. For example, passing a const statement as a function argument will produce an error. Or trying to assign the if statement to a variable. This just doesn't make sense in the language, because only expressions are expected in these cases:</p>
44 <p>Having this knowledge in mind, you will soon become the owner of two important superpowers:</p>
44 <p>Having this knowledge in mind, you will soon become the owner of two important superpowers:</p>
45 <ol><li>You will be able to see that most of the code, even that million-lines-12-year function, is just a bunch of things that become values.</li>
45 <ol><li>You will be able to see that most of the code, even that million-lines-12-year function, is just a bunch of things that become values.</li>
46 <li>You will be able to notice when code just won't work because it doesn't make sense in terms of expressions-vs-statements.</li>
46 <li>You will be able to notice when code just won't work because it doesn't make sense in terms of expressions-vs-statements.</li>
47 </ol>
47 </ol>