0

Currently using this to understand difference between expressions and statements because couldn't see an MDN overview on statements (that would be helpful).

My two questions are:

  1. An expression is something that resolves to a value. I think function expressions are assignment expressions. Is it an expression because when it's invoked it resolves to a value?

  2. I think 2 + 2 is not a statement. If it is not - why? A statement is loosely defined as an instruction to perform action.

  • The associated post has resolved my question. For '1', I now understand function (assignment) expressions return a function object which is assigned to a variable or property. As for '2' I learned that statements don't return a value (they perform an 'action'). Therefore, as `2 + 2` returns a value, it can't be a statement. – tonitone117 Jul 30 '20 at 00:11
  • `2 + 2;` is a statement (notice the semicolon in the end), so the expression `2 + 2` can form a statement as well. – Bergi Jul 30 '20 at 00:44
  • "*Is it an expression because when it's invoked it resolves to a value?*" - no, an expression resolves to a value when *evaluated*. A function expression is evaluated to a function object. – Bergi Jul 30 '20 at 00:46
  • A statement can be roughly interpreted to be **a line of code** if you interpret `;` to be end of line. Note that actual newlines may not end a line of code because statements may be spread across multiple lines. Since expressions are part of code expressions are part of statements. For example `2+2;` alone is a statement. `var x=2+2;` is a statement where `2+2` is the expression within that statement. Note that technically `(2+2);` alone is a statement that contains the expression `2+2` because in js braces forces something into an expression. – slebetman Jul 30 '20 at 01:18

0 Answers0