Suppose i declared an empty function on line-1:
1: function foo () {}
2: foo()
i executed the same same on line-2 , is line-2 a statement or expression and why?
Suppose i declared an empty function on line-1:
1: function foo () {}
2: foo()
i executed the same same on line-2 , is line-2 a statement or expression and why?
function foo () {} => this is a function declaration.
foo() => you are invoking that function , this is an expression to call that function
foo(), he's asking about the complete line 2.Line 2 is a statement that contains (and consists completely of) a call expression. It might be more easily identified as a statement had you not left out the optional semicolon at the end of the statement:
/* 1 */ function foo () {}
/* 2 */ foo();
// ^
The foo() part alone is the call expression which might also be used in a different context where expressions are allowed, e.g. as an argument to console.log in console.log(foo());.
The code on line 2 is a Call Expression (Sec 12.3.4).
However, the entirety of line 2 is an Expression Statement (Sec 13.5). Automatic Semicolon Insertion (Sec 11.9) is performed here to turn the expression into an expression statement.
A function call is an expression. It makes no difference what's in the body of the function being called. In fact, it makes no difference if the function is even defined (but if you try to execute the call before the function is defined, you'll get an error).
Any expression can also be used as a statement, simply by writing it by itself rather than as part of some other statement or expression.
So line 2 is both an expression and a statement.