Using older syntax
Prints "this is myfunc1"
myfunc1();
function myfunc1() {
console.log("this is myfunc1");
}
Using es6 syntax This gives error "myfunc2 is not a function"
myfunc2();
var myfunc2 = () => {
console.log("this is myfunc2");
}
Using older syntax
Prints "this is myfunc1"
myfunc1();
function myfunc1() {
console.log("this is myfunc1");
}
Using es6 syntax This gives error "myfunc2 is not a function"
myfunc2();
var myfunc2 = () => {
console.log("this is myfunc2");
}
Function declarations are hoisted.
Variable declarations using var
keyword are also hoisted (meaning the Javascript engine "knows" the variable has been declared) but, because assignments are not hoisted, the variable will contain undefined
until the line of code that does the assignment is executed.
Do not consider this "old" and "new" syntax.
Function declarations, function expressions, and arrow functions all have different behaviour. While arrow functions were introduced to the language more recently, they are not a replacement for function declarations or function expressions.