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

manoj ymkmanoj ymk
4 min read

πŸ”· 1. Core Concepts: Understand the Base Mechanics

TopicConcept
Function Declarationfunction name(params) { body } β€” creates a reusable block of code
Calling a FunctionfuncName(args) β€” runs the function
Local VariablesDeclared inside a function, not accessible outside
Outer VariablesAccessible inside the function unless shadowed
Global VariablesDeclared outside any function, available everywhere

πŸ”· 2. Parameter Handling

TypeBehavior
ParameterDeclared in function signature
ArgumentActual value passed during call
Missing ArgumentBecomes undefined unless a default value is provided
Default Parametersfunction fn(p = 5) β€” used if arg not passed or is undefined
Old-style Defaultsif (param === undefined) or `param = param
Nullish Coalescingparam = param ?? defaultVal (only null/undefined triggers default)

πŸ”· 3. Return Values

FeatureBehavior
return valueEnds 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

PrefixMeaning
showDisplays something (e.g., UI alert)
getRetrieves a value
setAssigns or updates a value
calcReturns computed value
checkReturns true/false
createMakes something new

🧠 Rule of Thumb:

πŸ“Œ "One function, one responsibility."


πŸ”· 5. Function Organization Best Practices

TipBenefit
Keep functions shortEasier to test and debug
Use descriptive namesCode reads like a story
Split big logic into small functionsImproves clarity and reuse
Think of functions like commentsGood 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

QuirkGotcha
return on a new lineJS inserts a semicolon β€” leads to empty return
Default parameters run on every callExpressions inside defaults are executed per call
Local variable shadows globalFunction will ignore global if a same-named local exists
Objects/arrays passed as arguments are by referenceModifying 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 TypeExamples
✍ SyntaxWhat’s the difference between parameter vs argument?
βš™ BehaviorWhat is the return value of a function with no return?
🧠 DebuggingWhy does my global variable change after calling a function?
πŸ’‘ TrickyWhat happens if I use return on a new line?

πŸ”· 10. Memory Hack Mnemonics

ConceptMnemonic
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

manoj ymk
manoj ymk