DEV Community

Cover image for All you need to know about Javascript's Expressions, Statements and Expression Statements

All you need to know about Javascript's Expressions, Statements and Expression Statements

Promise Tochi on October 21, 2017

By the end of this article, you should be able to describe in detail how the code in the image below works and why it works. There are two major...
Collapse
 
sebastiansimon profile image
Sebastian Simon • Edited

Expressions produce a value

There are a lot of misconceptions about what an expression really is. An expression is not actually required to produce a value. Either that, or, at the very least, it’s debatable.

For example:

Is new Array(-1) an expression? Sure looks like it. It throws a RangeError.

JSON.parse("x") is an expression, right? It throws a SyntaxError.

These errors can be caught with trycatch.

What about (() => {await x})? It’s a function expression wrapping another expression with await. All the parts are expressions (can you argue otherwise?). But, because await is not used top-level in an async function, it throws a SyntaxError in any context during parsing (so cannot be caught). Similarly: (function({}){"use strict";}).

This opens the question what kind of semantics are even applicable for the term “expression”. Is an expression a runtime thing? A syntax thing? Is it a thing before parsing? Or after parsing?

The ECMAScript specification, interestingly, doesn’t even define what an “expression” is in natural language. Instead, it provides grammar productions for “Expression”, defining it inductively, starting with the comma operator. It moves the question of what an “Expression” is further and further into less abstract questions, until it reaches concrete terminal symbols. For example an “Expression” is either an “AssignmentExpression” or an “Expression”, followed by a ,, followed by another “AssignmentExpression”. Then, similarly “AssignmentExpression” is defined, and so on.

Whether something is an expression cannot be determined without the full context (as demonstrated in the article with blocks vs. objects) and without building an AST (e.g. with AST explorer) (JS currently has no way of reflecting its own AST). On a mental level it’s an “I know it when I see it” kind of thing.


You will see that it returns 18 but despite that you cannot use it as an expression or where JavaScript expects a value. It is weird because you’d expect statements not to return anything, since the return value is pretty much useless if you cannot use it. That’s JavaScript for you, weird.

That is… not how things work. That statement does not return anything. It’s not a “weird” thing of JavaScript that a value is shown. This is the output of a JavaScript REPL. This output isn’t a return value, it’s the value from the completion record produced by the given code snippet. This completion record can only be accessed by a REPL, and only its value and (indirectly) its state can be accessed with the eval function, but only after its completion (e.g. eval("if(true){4;}") === 4, and if it doesn’t throw its state is “normal”). Also see Why does this dowhile loop repeat the last value after the end?.


So whatever is returned by the block statement, is implicitly coerced to 0 used as the operand.

No. There is no second operand on the left, there is no return value of the block, there is no 0. This is neither a binary + nor a binary -. It’s unary + and unary -.

Collapse
 
dudleycraig profile image
Dudley Craig • Edited

this article looked to be answering some queries i've had, tho got stopped at this ...
const foo = foo () => {
assignedVariable = 14
}
what does that mean? it doesn't compile ... is it an attempt to be a "named expression"? I'd assumed the arrow syntax is only for anonymous expressions?

again here, what's this? ... "foo(function () {} );"?

a bit ambiguous for me.

Collapse
 
paxfeline profile image
David Newberry

The code has one too many "foo"s, it should be:

const foo = () => {
assignedVariable = 14
}

() => { ... } is an anonymous function, which is then assigned to const foo.

The other code:
foo(function () {} );
also uses an anonymous function (using different syntax). This code would mean call the function "foo" and pass an anonymous (and empty) function.

Collapse
 
hayola profile image
Ayoola Moore

Hey - In quote - "A function declaration is a statement," I think a function declaration is an expression rather than a statement. Kindly refer to 3.2 of this article - 2ality.com/2012/09/expressions-vs-...

Also, with reference to your article here.
foo(if () {return 2})

the above code wouldn't work because the "if" within foo is a statement.

Look at this-
console.log(function(){}); // return the function. If function declaration is a statement, that would be invalid. As an expression is expected and I quote- "A value or string that can be used with replaceable parameters in the main message." ref: docs.microsoft.com/en-us/previous-...

Please let me know your thought. I'm just mastering every bit of Javascript. Thanks.

Collapse
 
aydinmuminkorcan profile image
Mümin Korcan Aydın

That was simply the best article about js expressions and statements I ve ever read , thank you

Collapse
 
promisetochi profile image
Promise Tochi

Thank you :)

Collapse
 
guid75 profile image
Guid75

Again, another reason why I deeply hate this language.

Thanks you.

Collapse
 
promisetochi profile image
Promise Tochi

Thanks for reading

Collapse
 
dailyscat profile image
Dailyscat • Edited

Hello tochi, I'm Eungyu Lee, korean Frontend developer. I think your writing is worth a lot. So I'd like to translate this and post it, would you let me?

Collapse
 
promisetochi profile image
Promise Tochi

Hi Eungyu, please go ahead. Can you share the translated with me if possible :)

Collapse
 
dailyscat profile image
Dailyscat

Thank you very much. Thanks to your post, more people are getting knowledge.
constds.tistory.com/123

Thread Thread
 
promisetochi profile image
Promise Tochi

Thank you

Collapse
 
alexandrutomescu profile image
Alexandru Tomescu

Thank you !

Collapse
 
cantfindaname88 profile image
Joe Buza

Fantastic article. I learned quite a bit. Very well written. Thank you.

Collapse
 
bellomuboye profile image
Bell Omuboye

What are the other categories. I have seen severally that these are the major ones but where can I see a full list