Reputation: 51
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();
Upvotes: 1
Views: 60
Reputation: 113
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);
Upvotes: 1
Reputation: 3832
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.
Upvotes: 2