- Definition: Functions are reusable blocks of code designed to perform a specific task. They help in reducing code repetition.
- Syntax:
function functionName(parameters) { // Code to be executed }
- Example:
function greet(name) { console.log("Hello, " + name + "!"); } greet("Alice");
- Definition: Functions can also be defined as expressions and assigned to variables.
- Syntax:
const functionName = function(parameters) { // Code to be executed };
- Example:
const greet = function(name) { console.log("Hello, " + name + "!"); }; greet("Bob");
- Definition: A shorter syntax for writing function expressions, introduced in ES6.
- Syntax:
const functionName = (parameters) => { // Code to be executed };
- Example:
const greet = (name) => { console.log("Hello, " + name + "!"); }; greet("Charlie");
- Parameters: Variables listed as part of the function definition.
- Arguments: Values passed to the function when it is invoked.
- Default Parameters: Functions can have default parameter values if no arguments are provided.
function greet(name = "Stranger") { console.log("Hello, " + name + "!"); } greet(); // Output: Hello, Stranger!
- Definition: Functions can return a value using the
return
statement. The function execution stops when thereturn
statement is reached. - Example:
function add(a, b) { return a + b; } const result = add(5, 3); // result = 8
- A callback function is a function passed as an argument to another function and is executed after the completion of that function.
- Example:
function greet(name, callback) { console.log("Hello, " + name); callback(); } function sayGoodbye() { console.log("Goodbye!"); } greet("Alice", sayGoodbye); // Output: Hello, Alice \n Goodbye!
- Callbacks are often used in asynchronous operations, such as reading files, making network requests, or setting timers.
- Example:
setTimeout(() => { console.log("This runs after 2 seconds"); }, 2000);
- Definition: The
map()
method creates a new array by applying a function to each element of an array. - Syntax:
const newArray = array.map(function(currentValue, index, arr), thisValue);
- Example:
const numbers = [1, 2, 3, 4]; const squares = numbers.map(number => number * number); console.log(squares); // [1, 4, 9, 16]
- Definition: The
filter()
method creates a new array containing elements that pass a test provided by a function. - Syntax:
const newArray = array.filter(function(currentValue, index, arr), thisValue);
- Example:
const numbers = [1, 2, 3, 4, 5]; const evenNumbers = numbers.filter(number => number % 2 === 0); console.log(evenNumbers); // [2, 4]
- Definition: The
reduce()
method executes a reducer function on each element of the array, resulting in a single output value. - Syntax:
const result = array.reduce(function(accumulator, currentValue, index, arr), initialValue);
- Example:
const numbers = [1, 2, 3, 4]; const sum = numbers.reduce((total, number) => total + number, 0); console.log(sum); // 10