JavaScript
Wat zijn functies en arrow functions?
Functies zijn reusable blocks code.
Function declaration vs expression
Declaration: function greet() { ... } - hoisted Expression: const greet = function() { ... } - niet hoisted Arrow: const greet = () => { ... } - moderne syntax
Arrow function nuances
Arrow syntax: (args) => { return value; } Short arrow (implicit return): (x) => x * 2 No arguments binding No own 'this' (inherits from parent scope)
Code Voorbeelden
JAVASCRIPTFuncties
// Declaration
function add(a, b) {
return a + b;
}
// Expression
const subtract = function(a, b) {
return a - b;
};
// Arrow function
const multiply = (a, b) => {
return a * b;
};
// Arrow with implicit return
const divide = (a, b) => a / b;
// No parameters
const greet = () => "Hallo!";
// Single parameter (parentheses optional)
const square = x => x * x;
// Default parameters
const power = (base, exp = 2) => base ** exp;
console.log(power(2)); // 4
console.log(power(2, 3)); // 8
// Rest parameters
const sum = (...numbers) => numbers.reduce((a, n) => a + n, 0);
console.log(sum(1, 2, 3, 4, 5)); // 15Relevante trefwoorden
functionarrow functionparameters