Functions
JavaScript Functions
In programming, a function is a reusable block of code that performs a specific task or set of tasks. Functions help modularize code, improve code organization, and promote code reuse. Here's an overview of functions in JavaScript:
Function Declaration:
In JavaScript, you can declare a function using the function
keyword. The basic syntax looks like this:
function functionName(parameters) {
// Code to be executed
return result; // Optional
}
- functionName: The name of the function.
- parameters: Parameters are variables that you define in the function declaration. They act as placeholders for values that will be passed when the function is called.
- return: The
return
statement is optional and is used to specify the value that the function should return.
Function Example:
function greet(name) {
return "Hello, " + name + "!";
}
let greeting = greet("John");
console.log(greeting); // Outputs: "Hello, John!"
Function Expression:
Another way to define a function is using a function expression. In this case, the function is assigned to a variable:
let multiply = function (a, b) {
return a * b;
};
let result = multiply(3, 4);
console.log(result); // Outputs: 12
Arrow Functions (ES6+):
Arrow functions provide a concise syntax for defining functions, especially for simple one-liner functions:
let square = (x) => x * x;
console.log(square(5)); // Outputs: 25
Function Parameters and Arguments:
- Parameters: These are the variables listed in the function declaration. They act as placeholders for the values that will be passed into the function.
- Arguments: These are the actual values passed to the function when it is called.
function add(a, b) {
return a + b;
}
let sum = add(2, 3); // Here, 2 and 3 are arguments
console.log(sum); // Outputs: 5
Default Parameters (ES6+):
You can provide default values for function parameters:
function greet(name = "Guest") {
return "Hello, " + name + "!";
}
console.log(greet()); // Outputs: "Hello, Guest!"
console.log(greet("John")); // Outputs: "Hello, John!"
Rest Parameters (ES6+):
The rest parameter allows a function to accept an indefinite number of arguments as an array:
function sum(...numbers) {
return numbers.reduce((acc, num) => acc + num, 0);
}
console.log(sum(1, 2, 3, 4)); // Outputs: 10
Function Scope:
Variables declared inside a function are typically local to that function, meaning they cannot be accessed outside of it.
Function Hoisting:
In JavaScript, function declarations are hoisted to the top of their scope, which means you can call a function before it is declared in the code.
Function Invocation:
A function is invoked or called to execute its code. You invoke a function by using its name followed by parentheses:
function sayHello() {
console.log("Hello!");
}
sayHello(); // Outputs: "Hello!"
Functions are fundamental to JavaScript and are a crucial part of building modular and maintainable code. They allow you to encapsulate logic, make code more reusable, and improve overall code organization.