JavaScript Functions
- Declaration:
function name() {} - Expression:
const name = function() {} - Arrow:
const name = () => {}
Fitur
- Default parameters
- Rest parameters (
...args) - Return value
- IIFE (Immediately Invoked Function)
function name() {}const name = function() {}const name = () => {}...args)function greet(name) { return "Hello, " + name + "!"; }
const add = (a, b) => a + b;
const welcome = (name = "Guest") => "Welcome, " + name + "!";
function sum(...nums) { return nums.reduce((a,b) => a+b, 0); }
const o = document.getElementById('output');
o.innerHTML =
'greet("World") → '+greet("World")+'
'+
'add(5, 3) → '+add(5,3)+'
'+
'welcome() → '+welcome()+'
'+
'welcome("Ali") → '+welcome("Ali")+'
'+
'sum(1,2,3,4,5) → '+sum(1,2,3,4,5);