๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท
โšก JavaScript

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.

5 min 10 XP Lesson 6 of 21
Functions
๐ŸŒ

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 use return
  • โ€ข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
  • โ€ขreturn is 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) => expression or (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
  • โ†’return is 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

โ€ข Event handler: <code>button.addEventListener('click', () => { handleSubmit(); });</code> โ€ข Map over data: <code>const doubled = nums.map(n => n * 2);</code> โ€ข Filter: <code>const adults = users.filter(u => u.age &gt;= 18);</code> โ€ข Async fetch: <code>const getData = async () => { const res = await fetch(url); ... };</code>

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

1 / 2

How do you return a value from a function?