Function Syntax
Function declaration
Section titled “Function declaration”// function declarationfunction myFunctionDeclaration(arg1, arg2) { console.log(arg1, arg2);}// function call:myFunctionDeclaration("param1", "param2");Function expression
Section titled “Function expression”// function expression (anonymous function, Zugriff über Variable, kein Hoisting)const myFunctionExpression = function (arg1, arg2) { console.log(arg1, arg2);}// function call:myFunctionExpression("param1", "param2");Arrow function
Section titled “Arrow function”// arrow function (anonymous function, Zugriff über Variable, kein Hoisting)const myArrowFunction = (arg1, arg2) => { console.log(arg1, arg2);}// function call:myArrowFunction("param1", "param2");
// different variants:const arrowFunctionOneArgParenthesis = (arg1) => { // do sth.;};const arrowFunctionOneArgNoParenthesis = arg1 => { // do sth.;};const arrowFunctionZeroArgsParenthesis = () => { // do sth.;};const arrowFunctionZeroArgsUnderscore = _ => { // do sth.;};Explicit and implicit return
Section titled “Explicit and implicit return”// explicit returnconst sumNormal = function (num1, num2) { return num1 + num2;}
// implicit return// the return statement is automatically created:// one line, no curly bracesconst sumArrow = (num1, num2) => num1 + num2;
// log the resultconst result = sumArrow(4, 18);console.log(result);Returning an object
Section titled “Returning an object”(is this also an implicit return?)
// returning an object: wrap the curly braces in parenthesesconst obj = value = ({key: value});