Functions are the primary building blocks of software. A function is a reusable, modular block of code designed to perform a specific task. By packaging code into functions, you avoid repeating logic (DRY: Don't Repeat Yourself) and create clean, testable programs.
In JavaScript, there are two traditional ways to define functions:
A. Function Declaration Uses the function keyword followed by the function name:
1 // 1. Defining the function
2 function greetStudent ( name ) {
3 console . log ( ` Hello, ${ name } ! Welcome to MSK Institute. ` ) ;
4 }
5
6 // 2. Calling (invoking) the function
7 greetStudent ( "Sumit" ) ;
B. Function Expression Assigns an anonymous (or named) function to a variable:
1 const calculateArea = function ( width , height ) {
2 return width * height ;
3 } ;
4
5 console . log ( calculateArea ( 5 , 10 ) ) ; // 50
Why does the choice between Declaration and Expression matter? Hoisting!
Function Declarations are Hoisted Completely: You can call a function declaration before the line where it is written:
1 sayHello ( ) ; // "Hello!" (Works perfectly!)
2
3 function sayHello ( ) {
4 console . log ( "Hello!" ) ;
5 }
Function Expressions are NOT Hoisted: Because the function is assigned to a const or let variable, it resides in the Temporal Dead Zone until initialized:
1 sayGoodbye ( ) ; // ReferenceError: Cannot access 'sayGoodbye' before initialization!
2
3 const sayGoodbye = function ( ) {
4 console . log ( "Goodbye!" ) ;
5 } ;
When a function is called in JavaScript:
1 A new Execution Context is created.
2 The function is pushed onto the top of the Call Stack .
3 Its code executes.
4 When it hits return or ends, it is popped off the Call Stack, and control returns to the caller.
1 function first ( ) {
2 console . log ( "1" ) ;
3 second ( ) ;
4 console . log ( "3" ) ;
5 }
6
7 function second ( ) {
8 console . log ( "2" ) ;
9 }
10
11 first ( ) ;
12 // Output order:
13 // 1
14 // 2
15 // 3
In JavaScript, functions are First-Class Citizens , which means functions are treated like any other value:
They can be assigned to variables. They can be stored in arrays and objects. They can be passed as arguments to other functions (Callbacks). They can be returned from other functions (Higher-Order Functions). 1 function executeTwice ( action ) {
2 action ( ) ;
3 action ( ) ;
4 }
5
6 function ringBell ( ) {
7 console . log ( "Ding dong! 🔔" ) ;
8 }
9
10 executeTwice ( ringBell ) ; // Rings twice!
Interactive Quiz • Knowledge Check
Practice Quiz What is the main benefit of organizing code into functions?
A It slows down program execution
B Code reusability, modularity, and avoiding repetition (DRY principle)
C It converts JavaScript into HTML
D It deletes global variables
What happens if you call a Function Declaration before the line where it is defined in code?
A A ReferenceError is thrown
B It executes normally because Function Declarations are hoisted completely to the top of scope
C The browser crashes
D It returns null
What happens if you call a Function Expression assigned to a const before its declaration?
A It runs normally
B It throws a ReferenceError because the variable is in the Temporal Dead Zone
C It prints undefined
D It restarts the computer
What does it mean that functions are "First-Class Citizens" in JavaScript?
A Only VIP developers can use them
B Functions can be stored in variables, passed as arguments, and returned from other functions just like numbers or strings
C Functions require a paid license
D Functions run with highest CPU priority
What is the syntax used to invoke (call) a function named init?
A call init;
B init();
C run(init);
D init{}