JavaScript Functions - Quick Guide
1. What is a Function?
A function in JavaScript is a block of code designed to perform a particular task. It is executed when
something invokes/calls it.
2. Function Declaration
Syntax:
function functionName(parameters) {
// code to be executed
Example:
function greet(name) {
[Link]('Hello ' + name);
greet('Sanjay'); // Output: Hello Sanjay
3. Function Expression
Syntax:
const functionName = function(parameters) {
// code to be executed
};
Example:
const add = function(a, b) {
return a + b;
};
JavaScript Functions - Quick Guide
[Link](add(2, 3)); // Output: 5
4. Arrow Functions (ES6)
Syntax:
const functionName = (parameters) => {
// code to be executed
};
Example:
const multiply = (a, b) => a * b;
[Link](multiply(4, 5)); // Output: 20
5. Function Parameters & Arguments
- Functions can have zero or more parameters.
- Arguments are the actual values passed when calling the function.
- You can provide default parameters:
function greet(name = 'Guest') {
[Link]('Hello ' + name);
greet(); // Output: Hello Guest
6. Types of Parameters & Arguments in JavaScript
1. **Positional Parameters:** The normal parameters listed in function parentheses, matched by position.
JavaScript Functions - Quick Guide
Example:
function sum(a, b) {
return a + b;
sum(2, 3); // Arguments passed positionally
2. **Default Parameters:** Parameters with default values used if no argument is passed.
Example:
function greet(name = 'Guest') {
[Link]('Hello ' + name);
3. **Rest Parameters:** Use `...` to gather all remaining arguments into an array.
Example:
function sumAll(...numbers) {
return [Link]((a, b) => a + b, 0);
sumAll(1, 2, 3, 4); // Output: 10
4. **Optional Parameters:** JavaScript functions do not enforce parameters, so any parameter can be
optional.
5. **Named Parameters via Object Destructuring:** Using objects as arguments allows named parameters.
Example:
function createUser({name, age, email}) {
[Link](name, age, email);
}
JavaScript Functions - Quick Guide
createUser({age: 30, email: 'a@[Link]', name: 'Sanjay'});
6. **Arguments Object:** Inside a function, `arguments` contains all passed arguments (not available in arrow
functions).
Example:
function showArgs() {
[Link](arguments);
showArgs(1, 2, 3);
7. Return Statement
- Functions can return a value using the return statement.
- After return, the function execution stops.
Example:
function square(num) {
return num * num;
[Link](square(6)); // Output: 36
8. Anonymous Functions
- Functions without a name.
- Usually used as arguments or stored in variables.
Example:
setTimeout(function() {
JavaScript Functions - Quick Guide
[Link]('Executed after 1 second');
}, 1000);
9. Immediately Invoked Function Expression (IIFE)
- A function that runs as soon as it is defined.
Example:
(function() {
[Link]('IIFE executed');
})();
10. Rest Parameters & Spread Operator
- Rest parameters allow a function to accept an indefinite number of arguments as an array.
Example:
function sum(...numbers) {
return [Link]((acc, val) => acc + val, 0);
[Link](sum(1, 2, 3, 4)); // Output: 10
11. Callback Functions
- A function passed as an argument to another function to be executed later.
Example:
function greetUser(name, callback) {
JavaScript Functions - Quick Guide
[Link]('Hello ' + name);
callback();
greetUser('Sanjay', function() {
[Link]('Callback executed!');
});