A function is defined using the function keyword, followed by a name, parentheses (), and a block of code {}.
function greet() {
console.log("Hello, welcome to JavaScript!");
}function greet() {
console.log("Hello, welcome to JavaScript!");
}function greet() {
console.log("Hello, welcome to JavaScript!");
}To execute a function, it must be called by its name with parentheses:
Functions can accept parameters (input values) and use them within their block.
function greetUser(name) {
console.log("Hello, " + name + "!");
}
greetUser("Alice"); function greetUser(name) {
console.log("Hello, " + name + "!");
}
greetUser("Alice"); function greetUser(name) {
console.log("Hello, " + name + "!");
}
greetUser("Alice"); Parameters are placeholders in the function definition.
Arguments are actual values passed to the function when called.
A function can have multiple parameters:
function addNumbers(a, b) {
console.log(a + b);
}
addNumbers(5, 10); function addNumbers(a, b) {
console.log(a + b);
}
addNumbers(5, 10); function addNumbers(a, b) {
console.log(a + b);
}
addNumbers(5, 10); A function can return a value using the return keyword.
function multiply(x, y) {
return x * y;
}
let result = multiply(4, 5);
console.log(result); function multiply(x, y) {
return x * y;
}
let result = multiply(4, 5);
console.log(result); function multiply(x, y) {
return x * y;
}
let result = multiply(4, 5);
console.log(result); Once return is executed, the function stops running.
Returned values can be stored in variables for later use.
Functions can be stored in variables as expressions.
const square = function(num) {
return num * num;
};
console.log(square(6)); const square = function(num) {
return num * num;
};
console.log(square(6)); const square = function(num) {
return num * num;
};
console.log(square(6)); Function expressions do not have a name (anonymous functions).
The function is called using the variable name.
Arrow functions provide a shorter syntax for writing functions.
const multiply = (a, b) => a * b;
console.log(multiply(3, 4));
const multiply = (a, b) => a * b;
console.log(multiply(3, 4));
const multiply = (a, b) => a * b;
console.log(multiply(3, 4));
If the function has only one statement, {} and return can be omitted.
If there is only one parameter, () can be omitted:
const greet = name => console.log("Hello, " + name);
greet("John"); const greet = name => console.log("Hello, " + name);
greet("John"); const greet = name => console.log("Hello, " + name);
greet("John"); Functions can have default parameter values that apply when no argument is provided.
function greet(name = "Guest") {
console.log("Hello, " + name);
}
greet();
greet("Alice"); function greet(name = "Guest") {
console.log("Hello, " + name);
}
greet();
greet("Alice"); function greet(name = "Guest") {
console.log("Hello, " + name);
}
greet();
greet("Alice"); The rest parameter syntax allows a function to accept multiple arguments as an array.
function sum(...numbers) {
let total = 0;
for (let num of numbers) {
total += num;
}
return total;
}
console.log(sum(1, 2, 3, 4, 5)); function sum(...numbers) {
let total = 0;
for (let num of numbers) {
total += num;
}
return total;
}
console.log(sum(1, 2, 3, 4, 5)); function sum(...numbers) {
let total = 0;
for (let num of numbers) {
total += num;
}
return total;
}
console.log(sum(1, 2, 3, 4, 5)); The ...numbers parameter gathers all arguments into an array.
Useful when the number of arguments is unknown.
A callback is a function passed as an argument to another function and executed later.
function processUser(name, callback) {
console.log("Processing user: " + name);
callback();
}
function done() {
console.log("User processed successfully.");
}
processUser("Alice", done);function processUser(name, callback) {
console.log("Processing user: " + name);
callback();
}
function done() {
console.log("User processed successfully.");
}
processUser("Alice", done);function processUser(name, callback) {
console.log("Processing user: " + name);
callback();
}
function done() {
console.log("User processed successfully.");
}
processUser("Alice", done);processUser calls done after printing a message.
Callbacks enable asynchronous programming and event handling.
An IIFE runs immediately after it is defined.
(function() {
console.log("This function runs immediately!");
})();(function() {
console.log("This function runs immediately!");
})();(function() {
console.log("This function runs immediately!");
})();Wrapped in parentheses to execute instantly.
Avoids polluting the global scope.
Functions are a fundamental part of JavaScript, allowing code reuse and modularity. The next section will explore objects and arrays, which are essential for organizing and managing data.