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...
For further actions, you may consider blocking this person and/or reporting abuse
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 aRangeError
.JSON.parse("x")
is an expression, right? It throws aSyntaxError
.These errors can be caught with
try
–catch
.What about
(() => {await x})
? It’s a function expression wrapping another expression withawait
. All the parts are expressions (can you argue otherwise?). But, becauseawait
is not used top-level in anasync
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.
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 thisdo
–while
loop repeat the last value after the end?.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-
.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.
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.
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.
That was simply the best article about js expressions and statements I ve ever read , thank you
Thank you :)
Again, another reason why I deeply hate this language.
Thanks you.
Thanks for reading
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?
Hi Eungyu, please go ahead. Can you share the translated with me if possible :)
Thank you very much. Thanks to your post, more people are getting knowledge.
constds.tistory.com/123
Thank you
Thank you !
Fantastic article. I learned quite a bit. Very well written. Thank you.
What are the other categories. I have seen severally that these are the major ones but where can I see a full list