Functions
๐ What are Functions in JavaScript? A function is a named, reusable block of code. JavaScript has two main syntaxes: function greet(name) {} (declaration) and const greet = (name) => {} (arrow function). Call them by name as many times as you need, passing different inputs each time.

Appy Saysโฆ
JavaScript has more ways to write functions than any other language. The modern one โ arrow functions โ is everywhere in professional code. Once you spot the pattern, you'll see it in every React app, every API call, every event handler.
Functions in JavaScript
JavaScript has three main ways to define functions โ they all do the same thing but look different:
- โขFunction declaration:
function greet(name) { return "Hi " + name; } - โขFunction expression:
const greet = function(name) { ... }; - โขArrow function:
const greet = (name) => "Hi " + name; - โขArrow functions are the modern standard โ short, clean, and used everywhere
Arrow functions โ the shortcut key
A full function declaration is like typing a full command in a game. An arrow function is like a keyboard shortcut โ same result, way less typing. (x) => x * 2 means 'take x, return x times 2'. Once you get used to them, regular functions feel verbose.
Arrow Function Syntax Rules
- โขOne parameter:
name => "Hi " + name(no brackets needed) - โขMultiple parameters:
(a, b) => a + b - โขMulti-line body: wrap in
{ }and usereturn - โขSingle expression: no
{ }needed โ the expression IS the return value - โขNo parameters:
() => console.log("hello")
Real-World Examples
- โขEvent handler:
button.addEventListener('click', () => { handleSubmit(); }); - โขMap over data:
const doubled = nums.map(n => n * 2); - โขFilter:
const adults = users.filter(u => u.age >= 18); - โขAsync fetch:
const getData = async () => { const res = await fetch(url); ... };
Key Facts
- โขFunction declarations are hoisted โ you can call them before they're defined in the file
- โขArrow functions and expressions are not hoisted โ define them before calling
- โข
returnis implied when there's no{}in an arrow function - โขFunctions are first-class values in JS โ you can pass them as arguments, store in variables, return from other functions
- โขCallbacks are functions passed as arguments โ used in event handlers, timers, fetch
Remember
Arrow functions don't have their own this โ this matters in classes and event handlers. For now, use arrow functions for callbacks and short utilities. Use regular functions or methods when you need this to refer to the current object.
What You Learned
- โขThree ways to define functions: declaration, expression, arrow
- โขArrow syntax:
(params) => expressionor(params) => { return ...; } - โขArrow functions are the modern standard โ used in React, APIs, callbacks
- โขFunctions are first-class values โ you can pass them around like variables
- โขCallbacks: functions passed as arguments to other functions
Key Facts
- โFunction declarations are hoisted โ you can call them before they're defined in the file
- โArrow functions and expressions are not hoisted โ define them before calling
- โ
returnis implied when there's no{}in an arrow function - โFunctions are first-class values in JS โ you can pass them as arguments, store in variables, return from other functions
- โCallbacks are functions passed as arguments โ used in event handlers, timers, fetch
Real-World Examples
Remember
Arrow functions don't have their own this โ this matters in classes and event handlers. For now, use arrow functions for callbacks and short utilities. Use regular functions or methods when you need this to refer to the current object.
Quick Quiz
How do you return a value from a function?