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

pushpesh kumarpushpesh kumar
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:

RuleExplanation
βœ… HoistedYou can call it before its definition
🧠 Scoped to nearest block or functionTraditionally, 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 requiredFunction declarations must have a name
πŸ” Can be recursiveName 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:

RuleExplanation
❌ Not hoistedMust be defined before use
βœ… Can be anonymous or namedUseful for debugging or recursion
🧠 ExpressionTreated 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:

RuleExplanation
βœ… Allowed only in expressionsCannot be used in declarations
⚠️ No internal nameHard to reference itself or show in stack trace
πŸ” Useful in callbacksCommon 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:

RuleExplanation
βœ… Name scoped only inside the functionsayHi 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/debuggingCan self-reference

3️⃣ Parameters, Arguments, and Return Values

πŸ”Ή Definitions

TermMeaning
ParameterVariable listed in function definition
ArgumentValue passed to the function
ReturnValue 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:

RuleBehavior
βœ… Parameters default to undefined if not passed
βœ… Can use default parameter values like function greet(name = "Guest")
βœ… Only one return per callreturn 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

FeatureFunction DeclarationFunction ExpressionAnonymous Function
Hoistedβœ… Yes❌ No❌ No
Namedβœ… Requiredβœ… Optional❌ No
Can be Recursiveβœ… Yesβœ… Yes (if named)❌ No
Use CaseGeneral logicCallbacks, assignmentShort 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

pushpesh kumar
pushpesh kumar