Search code examples
javascriptarrow-functions

JavaScript arrow function assignment


I tried googling that , but i can not discuss to google, sometimes in the courses i saw the instructor assign an arrow function to a variable like that.

const s = ( ) => { }

what are the cases when I need that syntax and not using

   function s( ) { }

My BASIC Question --> when to use

const s = ( ) => { } 

versus

function s( ) => { }

.. why the assignment ... thats my main Question (when and why i assign ?) why not use the arrow function without assigning it to a variable ??


Solution

  • Your examples are showing 2 ways to declare a function.

    This is an example of a function declaration.

    function s() {
    // some code
    }
    

    This is another way to define a function, called function expression.

    const s = function() {
    // some code
    }
    

    This is an arrow function. With the exception of the way this is treated between the arrow function and the other two, they are pretty much 3 ways to write the same function.

    const s = () => {
    // some code
    }
    

    As stated in the response below, function declaration and function expression are ES5 features and the arrow function is an ES6 feature.