Course JavaScript Fundamentals

Functions

Functions & Data Preview

Functions

Declaration & call

function greet(name) {\n  return `Hello, ${name}!`;\n}\n\nconsole.log(greet("Ama"));  // Hello, Ama!

Parameters & defaults

function total(items, discount = 0) {\n  return items - (items * discount);\n}\n\ntotal(100);     // 100\ntotal(100, 0.1); // 90

Arrow functions

const greet = (name) => `Hello, ${name}!`;\nconst double = (n) => n * 2;

Scope

Variables declared inside a function are local to it. Variables outside are global — keep globals rare.

Functions as values

function apply(fn, value) {\n  return fn(value);\n}\napply(double, 21);  // 42
Tip: Give functions verb names (calculateTotal, formatDate). A good name is better than a comment.

This is a free preview lesson

Enroll in the JavaScript Fundamentals to unlock every lesson, lesson PDF guides, quizzes and your certificate - and start creating your own pieces today.


Discussion

Free preview lesson