I was looking at some minified javascript code (from github), and the code has a block that looks like
h = function(a, b, c, d) {
var e, h, i, j, k, l, m = this;
return i = $("#js-frame-loading-template").clone().show(), l = c === "back" ? 350 : 500, j = i.find(".js-frame-loading-spinner").hide() // more stuff here
I’m curious why/how this code works since there are variable declarations after the return statement
Let me introduce you to the comma operator.
Everything to the right of the
returnstatement is one expression, consisting of multiple “sub-expressions”, separated by commas:Each sub-expression is evaluated, from left to right, and the value of the last one is the result of the whole expression. I.e. the result of the last expression is returned in this case (the results of evaluating
dand whatever hides in// more stuff herein your example).It’s a “trick” to squeeze multiple expressions into one.
Nitpick:
There are no variable declarations in that line, only assignment expressions. You could not have a
varstatement (a variable declaration) there because the comma operator (and thereturnstatement), only works with expressions, not statements.