- JS Introduction
- JS Introduction
- JS Comments
- JS Variables
- JS Datatypes
- JS Operators
- JS Type Conversions
- JS Control Flow
- JS Comparisons
- JS If else
- JS If else Ladder
- JS Ternary Operator
- JS Switch
- JS For Loop
- JS For In
- JS For Of
- JS While
- JS Do While
- JS Break & Continue
- JS Functions
- JS Function Declaration
- JS Function Parameters
- JS Return Statement
- JS Function Expressions
- JS Anonymous Functions
- JS Objects
- JS Objects
- JS Object Methods
- JS Object Constructors
- JS Object Destructuring
- JS Object Prototypes
- JS Map, Filter & Reduce
- JS ES6
- JS ES6
- JS let and const
- JS Arrow Functions
- JS Template Literals
- Destructuring Assignment
- JS Spread Operator
- JS Default Parameters
- JS Classes
- JS Inheritance
- JS Map
- JS Set
- JS Async
- JS Callbacks
- JS Asynchronous
- JS Promises
- JS Async/Await
- JS HTML DOM/BOM
- JS Document Object
- JS getElementbyId
- getElementsByClassName
- JS getElementsByName
- getElementsByTagName
- JS innerHTML
- JS outerHTML
- JS Window Object
- JS History Object
- JS Navigator Object
- JS Screen Object
JavaScript Function Expressions
- A function expression is a way to define a function as part of an expression.
- It results in the creation of an anonymous function (a function without a name) or a function with a specific name.
Syntax:
// Anonymous Function Expression
const functionName = function(parameter1, parameter2, /* ... */) {
// Code to be executed
};
// Named Function Expression
const addNumbers = function sum(a, b) {
return a + b;
};
- functionName: The variable that holds the function expression.
- parameter1, parameter2, ...: The parameters that the function expression accepts.
Why it is Used:
- Assigning Functions to Variables:Allows functions to be assigned to variables for dynamic behavior.
- Encapsulation: Supports creating functions within a specific scope, enhancing code encapsulation.
Example:
// Anonymous Function Expression
const greet = function(name) {
console.log('Hello, ' + name + '!');
};
// Calling the Function Expression
greet('John'); // Output: Hello, John!
Named Function Expression: In a named function expression, the function has a name that can be used within the function itself for recursion or debugging.
// Named Function Expression
const multiply = function product(a, b) {
return a * b;
};
// Calling the Named Function Expression
const result = multiply(3, 4);
console.log('Product:', result); // Output: Product: 12
Function Expressions as Arguments: Function expressions can be passed as arguments to other functions.
// Function Expression as Argument
const performOperation = function(callback) {
callback();
};
// Calling the Function with a Function Expression
performOperation(function() {
console.log('Performing the operation.');
});
Immediately Invoked Function Expression (IIFE): An IIFE is a function expression that is immediately executed after being defined.
// IIFE Example
(function() {
console.log('This is an IIFE.');
})();
Summary
- Function expressions are a way to define functions as part of expressions.
- They are useful for assigning functions to variables or passing functions as arguments.
- Named function expressions provide a function with a specific name for internal use.
- Immediately Invoked Function Expressions (IIFE) are used for immediate execution.
- Function expressions are often preferred in scenarios where dynamic behavior or encapsulation is needed.