⬅ Previous Topic
JavaScript FunctionsNext Topic ⮕
JavaScript Arrow Functions⬅ Previous Topic
JavaScript FunctionsNext Topic ⮕
JavaScript Arrow FunctionsA function expression in JavaScript is when a function is defined inside an expression, often assigned to a variable. Unlike function declarations, function expressions are not hoisted, making their position in code important for execution.
const greet = function() {
console.log("Hello, world!");
};
greet(); // Function call
Hello, world!
Let’s first understand the key difference between these two:
// Function Declaration
sayHello(); // Works - hoisted
function sayHello() {
console.log("Hello from declaration");
}
// Function Expression
sayHi(); // ❌ Error - not hoisted
const sayHi = function() {
console.log("Hi from expression");
};
Hello from declaration
Uncaught ReferenceError: Cannot access 'sayHi' before initialization
This is a function without a name, assigned to a variable.
const add = function(a, b) {
return a + b;
};
console.log(add(3, 4));
7
A function with a name, still assigned to a variable. Useful for recursion or debugging.
const factorial = function fact(n) {
if (n <= 1) return 1;
return n * fact(n - 1);
};
console.log(factorial(5));
120
Note: fact
is only available inside the function body, not outside.
Also known as self-executing functions, IIFEs run as soon as they are defined.
(function() {
console.log("IIFE runs instantly!");
})();
IIFE runs instantly!
function execute(callback) {
callback("JavaScript");
}
execute(function(name) {
console.log("Learning " + name);
});
Learning JavaScript
You can assign function expressions to object properties to define methods.
const person = {
name: "Alice",
greet: function() {
return "Hello, " + this.name;
}
};
console.log(person.greet());
Hello, Alice
Arrow functions provide a more concise syntax but come with some differences in this
binding.
const square = (x) => x * x;
console.log(square(5));
25
Arrow functions don’t have their own this
, which can be beneficial inside callbacks or when working with methods.
Aspect | Function Declaration | Function Expression |
---|---|---|
Hoisting | Yes | No |
Can be anonymous | No | Yes |
Executed immediately | No | Possible via IIFE |
Function expressions in JavaScript offer great flexibility. Whether you’re using them to encapsulate logic, pass behaviors, or write more modular code, mastering function expressions will help you write cleaner and more dynamic JavaScript.
In particular, pay attention to scope, hoisting, and how anonymous vs named expressions behave. Use IIFE when you want private scope execution. Prefer arrow functions for brevity and lexical scoping of this
.
// Task: Create an IIFE that prints the square of 8
Try solving it and explore how scoping works within function expressions.
⬅ Previous Topic
JavaScript FunctionsNext Topic ⮕
JavaScript Arrow FunctionsYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.