Functions
Functions
Function declaration
javascriptfunction add(a, b) { return a + b; } add(2, 3); // 5
Function expression
javascriptconst multiply = function (a, b) { return a * b; };
Arrow functions
javascriptconst add = (a, b) => a + b; const double = x => x * 2;
Short body can omit return. For a block body use { } and explicit return.
Parameters and arguments
Default parameters:
javascriptfunction greet(name = \"Guest\") { return `Hello, ${name}`; }
Rest parameters:
javascriptfunction sum(...nums) { return nums.reduce((a, b) => a + b, 0); }