News for hackers. Stuff that matters.
Re:Statements vs. Expressions by epolanski on Thursday July 14, 2022 @05:27AM (#32093820)
I feel the same towards the article, I think that the moment you need to resort to "think of it as..." it's the moment you lose me.

I also don't think he does a good job at explaining the biggest source of misunderstanding. Quoting the article:

`Expressions and statements are distinct things.`

That is not really correct.

A function call, e.g. is both an expression and a statement:

`foo(bar)` on it's own is both an expression (it returns a value, even if the value returned is undefined) and a statement.

In fact, determining if a piece of code is an expression or a statement in all borderline cases depends on the context.

```

const foo = function bar(){};

function bar(){}

```

On line 1 the bar function is an expression, on bar 3, is a statement.

There's nothing really about the function on it's own that can tell you whether it is an expression or a statement.

On the other hand, a nice way to differentiate statements from expressions is that you cannot use statements where you need expressions.

```

console.log(if (3 > 2) {})

const foo = while (i < 3) {} ```

both lines are syntax errors.

Re:Statements vs. Expressions by azangru on Thursday July 14, 2022 @05:51AM (#32093962)
I've never read the ECMAScript spec; but searching through it, I can see that the terms they use is function declaration vs function expression. According to their terminology, function bar(){} on the second line of your function example is a function declaration.
Re:Statements vs. Expressions by epolanski on Thursday July 14, 2022 @01:44PM (#32099216)
Yes, and a function declaration is a statement.