1

The simple definition of an expression is "something that resolves to a value." The simple definition of a statement is "an executable chunk of code."

With that in mind, since this function below resolves to a value of 6, does that make it an expression as well, instead of a statement, or both?

function ii () {
return 6;
}
ii();
Bail3y
  • 51
  • 5
  • 1
    Possible duplicate of http://stackoverflow.com/questions/1013385/what-is-the-difference-between-a-function-expression-vs-declaration-in-javascrip – Naga Sai A May 06 '17 at 20:05
  • Possible duplicate of [What is the difference between a function expression vs declaration in JavaScript?](http://stackoverflow.com/questions/1013385/what-is-the-difference-between-a-function-expression-vs-declaration-in-javascrip) – Alon Eitan May 06 '17 at 20:05
  • The function does not resolve to a value of 6. Why are you saying it does? You get the value 6 when you call it, but the function declaration does not produce a value. – Jeremy May 06 '17 at 20:09

2 Answers2

2

A function declaration defines what a function should do, as follows:

function ii () {
  return 6;
}

When that function is invoked as below, it becomes a function expression:

if ( ii() ) {
    console.log("true"); 
}
else
{
   console.log("false");
}

You can also have a function expression based on a declaration, as follows:

(function iii () {
  console.log(3);
  }()
 )

The following represents another kind of expression, for the value of a is the indicated function declaration:

var a = function iv() {
    return 5;
}

An interesting read on this topic is here.

slevy1
  • 3,797
  • 2
  • 27
  • 33
1

By your own references, no. The function itself does not resolve to anything, it just returns an already-resolved value. It's only a statement.

You are defining the function. That makes it a statement. Yes, you could say that calling the function "Resolves" it to the output, but it only triggers the output.

A value is more like an expression. It's not executable, it just is.

A getter is both an expression and a statement rolled into one:

Object.defineProperty(window, "ii", { get: function () { return 6 }});
// Returns '6'
ii;

ii = 7; 
// Logs '6' because setting 'ii' does not change the resolution function.
console.log(ii); 
Icepickle
  • 12,689
  • 3
  • 34
  • 48
imaxwill
  • 113
  • 8
  • Thank you for your answer. So by assigning the above function to a variable it becomes a function expression? Since the variable will then hold the function, the function is acting as a value & that makes it an expression? – Bail3y May 06 '17 at 20:36
  • @Icepickel I smoothed out the text a little, better? – imaxwill May 06 '17 at 21:25