Arrow functions

13 0 0
                                        

syntax 1:

     let name = () => "Hello World";

syntax 2:

    let name = (a) => 'Hello ${a}';

syntax 3:

     let name = (a, b) => {

         return '${a} ${b}';

     };

Arrow functions works like function expression. For example in the syntax 1:

     let name = function(){ return "Hello World"; };

In the right side of the arrow, is the statement you want to perform. If it is just one line code, there is no need to use a brace but if it is a multi line, you need a brace like in syntax 3 and you need a return statement for that.

================

let globalVariable = (param1) => 'From Mars ${param1}';

function connectingWorlds(){

    document.write(globalVariable("to Earth"));

}

================

HTML

connectingWorlds()

Result:

From Mars to Earth

Please take note of the globalVariable, the argument was put inside its parenthesis during invocation of the arrow function. If you just write: 

globalVariable(), the Result would be: From Mars undefined

globalVariable, the Result would be: (param1) => 'From Mars ${param1}';

Javascript ProgrammingWhere stories live. Discover now