News for hackers. Stuff that matters.
Re:Statements vs. Expressions by nivertech on Thursday July 14, 2022 @06:47AM (#32094272)
Not a JS-specific answer to this question:

expression

- generally returns a value, possibly a void value, or doesn't return control at all.

- it consists out of operators, operands, and function calls

- can be composed out of one or more sub-expressions, and the order of their evaluation isn't always defined (i.e. to allow compiler optimizations).

- usually it doesn't have side-effects (i.e. pure), unless it uses random, time, IPC, system, or HW-specific pseudo-functions, pseudo-variables, or macros as a sub-expressions. Nested assignments and variable increments/decrements/etc. are also not pure.

- a result of a function call

statement

- usually an imperative command

- usually indicate a side-effect (e.g. assignment, sending a message, etc.)

- can be a procedure call or macro

- can be composed out of expressions and nested statements, but with better defined semantics / order of evaluation

- a special case: control flow statements

- a special case: "return" statement - evaluate a value and returns it as a result of the current function

Re:Statements vs. Expressions by chrismorgan on Thursday July 14, 2022 @09:36AM (#32095931)
> statement

> - a special case: control flow statements

But it’s worth noting that this is not true in all languages: in some languages, almost everything or actually everything is an expression. In Rust, for example, control flow things may be statements or expressions (a mildly fuzzy distinction, but it comes down to whether it produces a value other than (), and whether it needs a semicolon after the closing curly brace), and some control flow things are very useful as expressions:

  let a = if b {
      c
  } else {
      d
  };

  let e = loop {
      break f;  // f is a value, not a label (which uses lifetime syntax, 'g).
  };
Re:Statements vs. Expressions by nivertech on Thursday July 14, 2022 @09:52AM (#32096107)
Ternary operator is an expression, first implemented as COND in Lisp.

In some cases it can be optimized to remove branching, especially when vectorized for SIMD/SIMT.

  int a,b,c,d;
  a = b ? c : d;

  // optimized to something like this:
  a = b*c + (1-b)*d

In some PLs even loop statements can produce values (CoffeeScript, Autocode), i.e. similar to list comprehensions, which are undoubtedly expressions.

I'm a kdb+/q developer, and most of the q code are expressions without conventional control flow operators (the only place they're used is outside of the main path: i.e. logging, testing, etc.).

IMO control flow (i.e. actual branching and looping) is just an implementation detail.