- 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 by assigning it to a variable or using it as part of a larger expression.
- Unlike a function declaration, which stands on its own, a function expression can be anonymous (no name) or named.
- In JavaScript, functions are "first-class citizens," meaning they can be treated like any other value—passed around, stored in variables, and returned from other functions.
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 reference to the function. You use this name to call the function later.
- parameter1, parameter2, ...: The inputs the function accepts, defined inside the parentheses.
ReferenceError.
Why it is Used:
- Assigning Functions to Variables: This makes your code more dynamic. You can swap the logic of a variable at runtime by assigning it a different function.
- Encapsulation: By keeping functions inside expressions or specific blocks, you avoid polluting the global namespace, which prevents naming conflicts in larger projects.
- Closures: Function expressions are essential when creating closures, allowing a function to "remember" the environment in which it was created.
Example:
In this basic example, we store a function that prints a message inside a variable called greet.
// Anonymous Function Expression
const greet = function(name) {
console.log('Hello, ' + name + '!');
};
// Calling the Function Expression
greet('John'); // Output: Hello, John!
const when defining function expressions. This ensures that the function reference cannot be accidentally overwritten or changed later in your script.
Named Function Expression: In a named function expression, the function has a name that is local only to its own scope. This is incredibly useful for recursion (when a function needs to call itself) or for clearer stack traces during debugging.
// Named Function Expression
const multiply = function product(a, b) {
// 'product' is available inside this function, but not outside
return a * b;
};
// Calling the Named Function Expression via the variable
const result = multiply(3, 4);
console.log('Product:', result); // Output: Product: 12
product(3, 4) would result in an error; you must use multiply(3, 4).
Function Expressions as Arguments: This is a common pattern in JavaScript, often referred to as a "callback." You pass a function into another function to be executed later.
// Real-world Example: Handling an event
const button = document.querySelector('#submit-btn');
// The second argument is a function expression
button.addEventListener('click', function() {
console.log('Button was clicked!');
});
// Example: Using a callback
const performOperation = function(callback) {
console.log('Starting operation...');
callback();
};
performOperation(function() {
console.log('Callback executed: Operation complete.');
});
Immediately Invoked Function Expression (IIFE): An IIFE is a function expression that runs as soon as the browser encounters it. It is wrapped in parentheses to tell the JavaScript engine to treat it as an expression, not a declaration.
// IIFE Example
(function() {
const temporarySecret = "I am hidden from the global scope";
console.log('This is an IIFE and it runs immediately.');
})();
// temporarySecret is not accessible here
Summary
- Flexibility: Function expressions define functions as values, allowing them to be stored in variables or objects.
- Scope Control: They are not hoisted, which encourages a more structured, top-to-bottom coding style.
- Naming: Named function expressions help with recursion and debugging, but the name is local to the function itself.
- Immediate Execution: IIFEs allow you to execute code immediately while keeping your variables private and out of the global scope.
- Modern Context: Function expressions are the foundation for Arrow Functions (
const fn = () => {}), which are widely used in modern frameworks like React and Vue.