Lesson 22: Mastering JavaScript Functions basics (Part-1)

4 min read
π· 1. Core Concepts: Understand the Base Mechanics
Topic | Concept |
Function Declaration | function name(params) { body } β creates a reusable block of code |
Calling a Function | funcName(args) β runs the function |
Local Variables | Declared inside a function, not accessible outside |
Outer Variables | Accessible inside the function unless shadowed |
Global Variables | Declared outside any function, available everywhere |
π· 2. Parameter Handling
Type | Behavior |
Parameter | Declared in function signature |
Argument | Actual value passed during call |
Missing Argument | Becomes undefined unless a default value is provided |
Default Parameters | function fn(p = 5) β used if arg not passed or is undefined |
Old-style Defaults | if (param === undefined) or `param = param |
Nullish Coalescing | param = param ?? defaultVal (only null/undefined triggers default) |
π· 3. Return Values
Feature | Behavior |
return value | Ends function and returns value |
No return / return; | Returns undefined |
Newline after return | 𧨠Syntax trap! Use: return (value) not return\nvalue |
π· 4. Function Naming Best Practices
Prefix | Meaning |
show | Displays something (e.g., UI alert) |
get | Retrieves a value |
set | Assigns or updates a value |
calc | Returns computed value |
check | Returns true/false |
create | Makes something new |
π§ Rule of Thumb:
π "One function, one responsibility."
π· 5. Function Organization Best Practices
Tip | Benefit |
Keep functions short | Easier to test and debug |
Use descriptive names | Code reads like a story |
Split big logic into small functions | Improves clarity and reuse |
Think of functions like comments | Good names = self-explanatory docs |
π· 6. Advanced Example: Prime Number Demo
function showPrimes(n) {
for (let i = 2; i < n; i++) {
if (!isPrime(i)) continue;
alert(i);
}
}
function isPrime(num) {
for (let i = 2; i < num; i++) {
if (num % i === 0) return false;
}
return true;
}
π Why this rocks:
showPrimes
handles display logic.isPrime
handles logic check.Each function does ONE thing.
π· 7. Common Pitfalls & Quirks
Quirk | Gotcha |
return on a new line | JS inserts a semicolon β leads to empty return |
Default parameters run on every call | Expressions inside defaults are executed per call |
Local variable shadows global | Function will ignore global if a same-named local exists |
Objects/arrays passed as arguments are by reference | Modifying them affects the original outside the function |
π· 8. Real-World Usage Examples
validateForm(formData)
sendNotification(user, message)
createButton(label)
getCurrentLocation()
checkLoginStatus(token)
π· 9. Interview Must-Know
Question Type | Examples |
β Syntax | Whatβs the difference between parameter vs argument? |
β Behavior | What is the return value of a function with no return? |
π§ Debugging | Why does my global variable change after calling a function? |
π‘ Tricky | What happens if I use return on a new line? |
π· 10. Memory Hack Mnemonics
Concept | Mnemonic |
Function Definition | "Fun starts with func!" |
Parameters vs Arguments | "Parameters are promises, Arguments are deliveries" |
Return Quirk | "Don't return and walk away!" β Stay on the same line |
Local scope | "What happens in a function, stays in the function." |
π· 11. Mind Map Overview
Functions
β
ββ Declarations
β ββ function name(params) { body }
β
ββ Variables
β ββ Local
β ββ Outer
β ββ Global
β
ββ Parameters
β ββ Default values
β ββ Nullish handling (??)
β ββ Legacy defaults (||)
β
ββ Return
β ββ return value
β ββ return;
β
ββ Naming
β ββ Prefixes (get, show, check)
β ββ Short, descriptive
β
ββ Best Practices
β ββ One function = one task
β
ββ Debug & Traps
ββ Return newline issue
ββ Shadowing variables
0
Subscribe to my newsletter
Read articles from manoj ymk directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
