MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Defining and Calling Functions0%
Study Materials
JavaScript for BeginnersProgress 0%
1.1What is JavaScript?1.2How JavaScript Works in Browser1.3Setting up Environment: VSCode, Live Server
7.1Defining and Calling Functions7.2Function Parameters and Return Values7.3Arrow Functions (Basic)
HomeTutorialsJavaScript for BeginnersDefining and Calling Functions
Function Parameters and Return Values

Defining and Calling Functions

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study Materials

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.


1. Function Declarations vs. Function Expressions

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
2function greetStudent(name) {
3 console.log(`Hello, ${name}! Welcome to MSK Institute.`);
4}
5 
6// 2. Calling (invoking) the function
7greetStudent("Sumit");

B. Function Expression

Assigns an anonymous (or named) function to a variable:

1const calculateArea = function(width, height) {
2 return width * height;
3};
4 
5console.log(calculateArea(5, 10)); // 50

2. Hoisting Differences (Crucial Interview Concept!)

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:

1sayHello(); // "Hello!" (Works perfectly!)
2 
3function 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:

1sayGoodbye(); // ReferenceError: Cannot access 'sayGoodbye' before initialization!
2 
3const sayGoodbye = function() {
4 console.log("Goodbye!");
5};

3. Function Execution & The Call Stack

When a function is called in JavaScript:

  1. 1
    A new Execution Context is created.
  2. 2
    The function is pushed onto the top of the Call Stack.
  3. 3
    Its code executes.
  4. 4
    When it hits return or ends, it is popped off the Call Stack, and control returns to the caller.
1function first() {
2 console.log("1");
3 second();
4 console.log("3");
5}
6 
7function second() {
8 console.log("2");
9}
10 
11first();
12// Output order:
13// 1
14// 2
15// 3

4. Functions as First-Class Citizens

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).
1function executeTwice(action) {
2 action();
3 action();
4}
5 
6function ringBell() {
7 console.log("Ding dong! 🔔");
8}
9 
10executeTwice(ringBell); // Rings twice!

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What is the main benefit of organizing code into functions?
Question 2
What happens if you call a Function Declaration before the line where it is defined in code?
Question 3
What happens if you call a Function Expression assigned to a const before its declaration?
Question 4
What does it mean that functions are "First-Class Citizens" in JavaScript?
Question 5
What is the syntax used to invoke (call) a function named init?
Next Lesson

Function Parameters and Return Values

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Template Literals (Basics)Function Parameters and Return Values

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Beginners with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext