π JavaScript Functions Deep Dive #1: How Functions Are Defined, Named, and Used

4 min read
Functions are at the heart of JavaScript β powering everything from event handling to API interactions. In this article, weβll dive deep into:
Function declarations vs expressions
Named vs anonymous functions
Parameters, arguments, and return values
1οΈβ£ Function Declarations vs Function Expressions
β Function Declaration
function greet(name) {
return `Hello, ${name}`;
}
console.log(greet("Alice")); // "Hello, Alice"
π Language Rules:
Rule | Explanation |
β Hoisted | You can call it before its definition |
π§ Scoped to nearest block or function | Traditionally, function declarations were only allowed at the top level (global or inside a function).Since ES6, JavaScript allows function declarations inside blocks like if , for , etc., and those functions are scoped only to that block (just like let and const ). "Scoped to the nearest block or function" means Itβs accessible only within the block or function where it's defined. |
β οΈ Name required | Function declarations must have a name |
π Can be recursive | Name allows internal recursive calls |
β Example:
console.log(add(2, 3)); // β
Works
function add(x, y) {
return x + y;
}
β Function Expression
const greet = function(name) {
return `Hi, ${name}`;
};
console.log(greet("Bob")); // "Hi, Bob"
π Language Rules:
Rule | Explanation |
β Not hoisted | Must be defined before use |
β Can be anonymous or named | Useful for debugging or recursion |
π§ Expression | Treated like any variable or constant |
β Common Mistake:
console.log(add(2, 3)); // β Error: Cannot access before initialization
const add = function (x, y) {
return x + y;
};
2οΈβ£ Anonymous vs Named Functions
πΈ Anonymous Function
A function without a name, often used in callbacks or expressions:
setTimeout(function () {
console.log("Timer done");
}, 1000);
π Language Rules:
Rule | Explanation |
β Allowed only in expressions | Cannot be used in declarations |
β οΈ No internal name | Hard to reference itself or show in stack trace |
π Useful in callbacks | Common in array methods, event handlers, timeouts |
πΈ Named Function Expression
const greet = function sayHi(name) {
return `Hello, ${name}`;
};
console.log(greet("Tom")); // β
Works
// sayHi("Tom"); // β ReferenceError: sayHi is not defined
const greetings = function sayHello(name) {
if (!name) return sayHello("Guest"); // β
works here
return `Hello, ${name}`;
};
π Language Rules:
Rule | Explanation |
β Name scoped only inside the function | sayHi is available only within its own body.Inside the function, you can use name of the function to reference itself β useful for recursion or internal debugging. |
β Used for recursion/debugging | Can self-reference |
3οΈβ£ Parameters, Arguments, and Return Values
πΉ Definitions
Term | Meaning |
Parameter | Variable listed in function definition |
Argument | Value passed to the function |
Return | Value the function gives back |
β Example:
function multiply(x, y) { // x, y are parameters
return x * y; // return value
}
console.log(multiply(3, 4)); // 12. Here 3, 4 are arguments
πΈ Language Rules for Parameters & Return:
Rule | Behavior |
β
Parameters default to undefined if not passed | |
β
Can use default parameter values like function greet(name = "Guest") | |
β Only one return per call | return ends function execution immediately |
β
Functions without return return undefined by default |
π§ͺ Advanced Tip: Default Parameters
function say(message = "Hi", name = "Guest") {
return `${message}, ${name}`;
}
console.log(say()); // Hi, Guest
function showValue(x = 10) {
console.log(x);
}
showValue(undefined); // β
10 (default kicks in)
showValue(); // β
10 (default kicks in)
showValue(null); // β null (no default)
showValue(0); // β 0 (no default)
showValue(false); // β false (no default)
β
Default values only kick in when the argument is undefined
(not null
or falsy).
π§ Bonus: Arrow Functions (Sneak Peek)
You'll often see anonymous function expressions written with arrow functions:
const greet = (name) => `Hi, ${name}`;
Weβll dive into arrow functions (and their this
behavior) in the next article.
β Recap Table
Feature | Function Declaration | Function Expression | Anonymous Function |
Hoisted | β Yes | β No | β No |
Named | β Required | β Optional | β No |
Can be Recursive | β Yes | β Yes (if named) | β No |
Use Case | General logic | Callbacks, assignment | Short inline use |
0
Subscribe to my newsletter
Read articles from pushpesh kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
