const es6Function = (variable) => {
// function logic goes here}
The above is a function declaration, variable declaration, and a variable assignment at the same time.
No, it’s not a function declaration.
It may seem to be a pedantic distinction, but hey, you’re the person who decided to write about this. Something is a function declaration — this is a term which has a precise meaning defined in the spec — if and only if it uses the function
keyword, and is not in a position where it is would have to be an expression (such as inside parentheses, or on the RHS of an assignment). Among other reasons, this is important because function declarations have specific hoisting behavior. For instance, you could write function es6Function(variable) { }
at the end of your enclosing function scope, and it would be “hoisted”, meaning it would be available at any point in the function, which would not be the case if you placed your assignment of a function expression at the end of the function. (Technically, if you had used var
, then the variable name itself would be hoisted and considered “in scope” at the beginning of the function, but the assignment would not take place until execution reached the lexical location of the declaration/assignment, until which point it would have the value undefined
. With const
, as in your example, it’s not hoisted at all, meaning that references to the variable es6Function
if declared at the end of the enclosing function scope would yield a ReferenceError
.
Your example is a variable declaration with that variable being assigned (initialized) to a function expression. Therefore, to return to the original point of this thread, there’s no need to make any special distinction here for purposes of talking about whether or not semi-colons are mandatory, or optional, or desirable. It’s just an expression, like any other; it just happens to be a function expression, instead of, say, a boolean expression. This is no different whether it’s ES3, ES5, S6, ES7, or ES8, or a function expression using the function
keyword, or an arrow function.
I’m still eager to hear what syntax you think was removed or deprecated in ES6.