Skip to content

Function Syntax

// function declaration
function myFunctionDeclaration(arg1, arg2) {
console.log(arg1, arg2);
}
// function call:
myFunctionDeclaration("param1", "param2");
// 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 (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 return
const sumNormal = function (num1, num2) {
return num1 + num2;
}
// implicit return
// the return statement is automatically created:
// one line, no curly braces
const sumArrow = (num1, num2) => num1 + num2;
// log the result
const result = sumArrow(4, 18);
console.log(result);

(is this also an implicit return?)

// returning an object: wrap the curly braces in parentheses
const obj = value = ({key: value});