0

We know that an expression is any piece of code that evaluates in a way we can say to a single value in Js, so it can perform a calculation in order to get the value or it can already be the value so the final product, so knowing this why we refer to this type of function that can be store in a variable as a function expression? So if we think this when we run this:

const calcAge = function (birthYear) {
  return 2037 - birthYear;
 };

this only creates the function, this actually doesn't perform any calculation in order to produce a value, and expression would be the function call cause this process at the end is a single value, so? or is it that the function itself is the value? what really mean the word "expression" in this definition? please someone can clarify this to me, thanks!

  • 7
    A function expression is an expression whose value is a function, just as a numeric expression is an expression whose value is a number. – Barmar Mar 29 '23 at 20:42
  • Literals like `'something'` or `10` are expressions as well hence a function that can be assigned to a variable is an expression – Konrad Mar 29 '23 at 20:42
  • 3
    Probably a better term would be "function literal", but that's not what caught on. – Barmar Mar 29 '23 at 20:42
  • 1
    Like the above comments, and also if it makes it easier for you an expression is a piece of code that can be assigned to a variable and it can be passed around, but the whole declaration `const calcAge = function...` is a statement. A statement has placeholders for expression(s). A statement doesn't return/evaluate to a value. An expression evaluates to a value. You can use `calcAge`'s value - *a function expression* - and pass it as argument (*callback fn*) to a FN, ex.: `console.log()` it. But, you can't use a statement as an argument, because again, it doesn't executes/evaluates to a value. – Aleksandar Mar 29 '23 at 21:03
  • Relevant docs: [Expressions and operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators) – pilchard Mar 29 '23 at 21:16
  • If to answer your exact question is that:if function-expression call returns yet another value/expression it doesn't make it less of an expression.Like you can use an IIFE (immediately invoked function expression) and it's still an expression. That aside, the passing it as argument helped me when I once struggled why I couldn't use `if` statement in my `{}` in React's JSX but I could use *ternary operator* it's that it gets executed right away similarly to IIFE, hence why the *ternary operator* is able to be used in JSX's JS-expression placeholders (`{}`) the same way it can be used as FN arg. – Aleksandar Mar 29 '23 at 21:18
  • "*this only creates the function, this actually doesn't perform any calculation in order to produce a value*" - that *is* performing operations to produce the function. – Bergi Mar 29 '23 at 21:49
  • "*what really mean the word "expression" in this definition?*" - see https://en.wikipedia.org/wiki/Expression_(computer_science) – Bergi Mar 30 '23 at 00:04

1 Answers1

-2

Basically this would define same function but in different way - this naming, that is confusing - function expression - simply means (quote form MDN with link)

The function keyword can be used to define a function inside an expression

MDN #1

And other way to declare function - function declarations

The function declaration defines a function with the specified parameters.

MDN #2

E.g. all the functions here same - the difference is the way of declaration

Hope that helps to clarify

ByteMaster
  • 172
  • 6
  • 2
    *'all the functions here same - the difference is the way of declaration'* This isn't accurate and the links you've provided explain those differences. Most notably, [function expressions aren't hoisted](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function#function_expression_hoisting). – pilchard Mar 29 '23 at 21:11