You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Functions are reusable blocks of code that perform a specific task. They are one of the most important concepts in JavaScript — almost everything you build will be organised into functions.
The classic way to define a function:
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("Alice")); // "Hello, Alice!"
return statement sends a value back to the caller.return is specified, the function returns undefined.A function assigned to a variable:
const greet = function(name) {
return "Hello, " + name + "!";
};
console.log(greet("Bob")); // "Hello, Bob!"
Function expressions are not hoisted — you must define them before you use them.
Introduced in ES6, arrow functions provide a shorter syntax:
// Standard arrow function
const greet = (name) => {
return "Hello, " + name + "!";
};
// Concise body (implicit return for single expressions)
const double = (x) => x * 2;
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.