0

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?

1
  • Why do you think it matters if the function is empty or not? The call of a function is the same regardless of the function's body. Commented Aug 16, 2018 at 19:40

4 Answers 4

2

function foo () {} => this is a function declaration.

foo() => you are invoking that function , this is an expression to call that function

Sign up to request clarification or add additional context in comments.

1 Comment

OP isn't asking about foo(), he's asking about the complete line 2.
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());.

2 Comments

Thanks. I was thinking I had a fundamental misunderstanding about how ECMAScript works after reading all those answers that steadfastly claimed it to be an expression when I was convinced it was an expression statement.
@JörgWMittag After reading all those answers, I felt compelled to write my own :-)
1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.