Understanding Function Expression in Javascript - ES6

 Functions - It can be written in three ways in JavaScript. we developers always think of writing it in old ways which is called as 'Function Declaration way' ex : 


    -It can be also written in variable assignment way. This is called 'Function expression'   ex: 

                                 


The main difference between a function expression and a function declaration is the function name, which can be omitted in function expressions to create anonymous functions.

Function expressions in JavaScript are not hoisted, unlike function declarations. You can't use function expressions before you create them:



- Naming functions is useful if they need to reference themselves (e.g. for recursive calls). Indeed, if you are passing a literal function expression as an argument directly to another function, that function expression cannot directly reference itself in ES5 strict mode unless it is named.

For example, consider this code:

let result = setTimeout(function sayMoo() {
    console.log("hi");
    setTimeout(sayMoo, 1000);
}, 1000);

console.log(result);

       

Comments

Popular posts from this blog

Understanding Collection and Object Initializer

How to execute Javascript in VS code