LCWD LogoLearn Code With DurgeshLCWD
HomeCoursesTutorialsBlogsContact
About LCWDAbout Durgesh Tiwari
Flex Box Hero
LearnCodeWithDurgesh Logo

Learn Code With Durgesh

Offering free & premium coding courses to lakhs of students via YouTube and our platform.

Explore

  • About Us
  • Courses
  • Blog
  • Contact
  • FlexBox Game

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Support

Contact

  • 📞 +91-9839466732
  • [email protected]
  • Substring Technologies, 633/D/P256 B R Dubey Enclave Dhanwa Deva Road Matiyari Chinhat, Lucknow, UP, INDIA 226028
© 2025 Made with ❤️ by Substring Technologies. All rights reserved.
Mastering JavaScript: Functions, Objects, Classes & Prototypes Explained Like Never Before

Mastering JavaScript: Functions, Objects, Classes & Prototypes Explained Like Never Before

By Shruti • Mon Aug 18 2025

Mastering JavaScript: Functions, Objects, Classes & Prototypes Explained Like Never Before

If you’re learning JavaScript, you’ve probably heard terms like functions, objects, classes, constructors, inheritance, and prototypes being thrown around. But what do they really mean? And why are they so important in JavaScript?

In this blog, we’ll break down these concepts step by step, with clear explanations, examples, comparisons, and diagrams. By the end, you’ll have a solid understanding of how these features work together — and why they are the backbone of modern JavaScript.


 1. Functions in JavaScript

 What is a Function?

A function is a reusable block of code designed to perform a particular task. Instead of writing the same code multiple times, we wrap it inside a function and call it whenever needed.

 Syntax

function greet() {
    console.log("Hello!");
}
greet(); // Hello!

Types of Functions

Named Function

function sayHi() {
    console.log("Hi!");
}
sayHi();

Anonymous Function (function without a name, often stored in variables)

const sayHello = function() {
    console.log("Hello!");
};
sayHello();

Arrow Function (ES6+)

const greet = () => {
    console.log("Greetings!");
};
greet();

Normal Function vs Arrow Function

FeatureNormal FunctionArrow Function
SyntaxLonger (function)Shorter (()=>{})
this bindingHas its own this (depends on how function is called)Inherits this from surrounding scope (lexical this)
HoistingCan be hoisted (declared before usage)Cannot be hoisted (must be defined before use)
Best Use CaseFor methods inside objects & when you need this contextFor callbacks, array methods (map, filter, reduce)

Use normal functions for object methods and arrow functions for callbacks or short tasks.

Callback Functions

A callback function is a function passed as an argument to another function. They’re essential in asynchronous programming (e.g., setTimeout, API calls).

function processUserInput(callback) {
    const name = "John";
    callback(name);
}

processUserInput((name) => {
    console.log("Hello " + name);
});
// Output: Hello John

Callbacks are often used in event handling, API requests, and functional programming.

2. Objects in JavaScript

 What is an Object?

An object is a collection of key-value pairs (properties and methods).

const person = {
    name: "John",
    age: 30,
    greet: function() {
        console.log("Hi, I am " + this.name);
    }
};
person.greet(); // Hi, I am John

Why Objects Are So Powerful

  1. Everything in JavaScript is an object (arrays, functions, dates).

  2. Objects allow grouping data & behavior together (properties + methods).

  3. Objects are dynamic — you can add or remove properties anytime.

  4. Objects power JSON, the most popular data format for APIs.

  5. Objects are the foundation for OOP (Object-Oriented Programming) in JS.

3. Classes in JavaScript

Classes were introduced in ES6 to make OOP more intuitive. They are essentially syntactic sugar over constructor functions.

class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
    greet() {
        console.log(`Hello, I am ${this.name}`);
    }
}
const p1 = new Person("Alice", 25);
p1.greet();

4. Constructor vs Constructor Function

  • Constructor (inside a class) → A special method in ES6 classes used for initializing objects.

  • Constructor Function (ES5) → A normal function that creates objects when called with new.

// Constructor Function (ES5)
function Animal(type) {
    this.type = type;
}
const dog = new Animal("Dog");

// Constructor (ES6 Class)
class Car {
    constructor(model) {
        this.model = model;
    }
}
const tesla = new Car("Model 3");
ConceptDefinitionExampleUse Case
ClassBlueprint for creating objects (ES6)class Person {}Structured OOP
ObjectInstance containing properties & methodsconst obj = {}Store & manipulate data
Constructor (class)Special method in class to initialize objectconstructor(name){}Used in ES6+
Constructor Function (ES5)Function that acts like a class before ES6function Car(){}Legacy code, older browsers
PrototypeShared object from which others inheritDog.prototype.speakEnables inheritance
InstanceActual object created from a class/constructornew Car()Real usable entity

5. Inheritance

Inheritance allows a child class to reuse properties and methods of a parent class.

class Animal {
    constructor(name) {
        this.name = name;
    }
    speak() {
        console.log(`${this.name} makes a sound.`);
    }
}
class Dog extends Animal {
    speak() {
        console.log(`${this.name} barks.`);
    }
}
const d = new Dog("Buddy");
d.speak();

6. Prototypes in JavaScript

All JavaScript objects have a prototype, which is another object they inherit from.

const arr = [1, 2, 3];
console.log(arr.__proto__ === Array.prototype); // true

Prototype Chain Diagram

arr
 |
 └── __proto__ → Array.prototype
                      |
                      └── __proto__ → Object.prototype
                                              |
                                              └── null

7. Summary Table (ES5 vs ES6)

ConceptES5ES6+
Functionfunction(){}Arrow ()=>{}
Objectvar obj={}Same
ClassConstructor functionclass {}
ConstructorNormal functionconstructor()
InheritancePrototype chainextends & super()

8. Practice Assignments

  1. Create a Student class with introduce() method.

  2. Extend it to CollegeStudent with collegeName.

  3. Write a constructor function Book with summary() prototype method.

  4. Build an object laptop with specs() method.

  5. Create a callback-based function fetchData(cb) that logs fetched data.

9. Mini Project Idea for you – Zoo Management System

Use classes + inheritance to model animals (Mammals, Birds, Reptiles) with unique behaviors.

Summary 

JavaScript’s functions, objects, classes, constructors, and prototypes are not separate pieces but parts of one ecosystem. Functions allow logic reusability, objects provide data structure, classes give blueprints, and prototypes ensure inheritance.

If you master these, you’ll unlock the real power of JavaScript and be ready to build complex, scalable applications.

 

 

Share this article ...

💬WhatsApp📘Facebook💼LinkedIn🐦X

Trending Blogs...

AI: Boon or Curse? Advantages & Disadvantages Explained

AI: Boon or Curse? Advantages & Disadvantages Explained

Artificial Intelligence (AI) is no longer just a concept of the future—it is already shaping our present. From asking Alexa or Siri to play music, using Google Maps for directions, or experiencing personalized recommendations on Netflix, AI has become a part of our everyday life.

The Future of Technology: Quantum Computing and Its Ecosystem

The Future of Technology: Quantum Computing and Its Ecosystem

Quantum computing isn’t just another buzzword—it’s a completely new way of thinking about computers. Unlike our everyday laptops or phones that run on classical computing, quantum computers use the strange but powerful rules of quantum mechanics.

Tailwind CSS Cheat Sheet – Complete Utility Class Guide 2025

Tailwind CSS Cheat Sheet – Complete Utility Class Guide 2025

Tailwind CSS Cheat Sheet – Complete Utility Class Guide 2025

Expert-Level JavaScript Interview Q&A for 2025: Crack Advanced JS Interviews

Expert-Level JavaScript Interview Q&A for 2025: Crack Advanced JS Interviews

Get ready for tough coding interviews with this collection of advanced JavaScript interview questions and answers. Designed for experienced developers who want to master complex JS concepts and impress recruiters.

Ace Your Coding Interview: JavaScript Q&A for Intermediate Learners

Ace Your Coding Interview: JavaScript Q&A for Intermediate Learners

Prepare for your next coding interview with this collection of JavaScript interview questions and answers for intermediate developers. Strengthen your JS concepts and boost your confidence.

Most Asked JavaScript Interview Questions with Answers

Most Asked JavaScript Interview Questions with Answers

Q1. What is JavaScript? JavaScript is a programming language mainly used to make web pages interactive and dynamic. It runs in the browser, and with Node.js, it can also run on the server.

Share this article ...

💬WhatsApp📘Facebook💼LinkedIn🐦X