LCWD LogoLearn Code With DurgeshLCWD
HomeCoursesHandbooksBlogsContact
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...

I Used These 3 ChatGPT Prompts Daily in 2025 – Saved 3 Hours

I Used These 3 ChatGPT Prompts Daily in 2025 – Saved 3 Hours

Discover 3 powerful ChatGPT prompts that save you hours daily. Boost your productivity with AI in 2025.

Top 10 CSS Tips to Instantly Improve Your Design Skills (Beginner Friendly)

Top 10 CSS Tips to Instantly Improve Your Design Skills (Beginner Friendly)

Before writing CSS, know how it connects with HTML and why using external stylesheets is best.

Best Investment Apps in India 2025 – Start Investing Smartly from ₹100

Best Investment Apps in India 2025 – Start Investing Smartly from ₹100

Investing today has become easier than ever — thanks to powerful mobile apps that put the entire stock market, mutual funds, and digital assets right in your pocket .

Top 10 Highest-Paying Skills to Learn in 2025 (Earn ₹15–50 Lakh/Year)

Top 10 Highest-Paying Skills to Learn in 2025 (Earn ₹15–50 Lakh/Year)

Hey there! If you’re feeling the pressure of keeping up with today’s crazy-fast job market, you’re not alone. AI, automation, and digital transformation are flipping industries upside down, and the skills that used to land you a solid gig might not cut it anymore.

iPhone 17 Series Launch 2025 – India Price, Specs, Pros & Cons

iPhone 17 Series Launch 2025 – India Price, Specs, Pros & Cons

Apple has officially unveiled the **iPhone 17 series** on **September 9, 2025**, at its **“Awe Dropping” event**. The lineup includes **iPhone 17, iPhone 17 Air, iPhone 17 Pro, and iPhone 17 Pro Max**. With **120 Hz displays across all models, ultra-thin design, A19 chips, and camera upgrades**, Apple is raising the bar again.

Async/Await vs Promises in JavaScript – Which Should You Use in 2025?

Async/Await vs Promises in JavaScript – Which Should You Use in 2025?

Asynchronous programming is one of the most important topics in modern JavaScript development. Whether you’re building a web app, mobile application, or integrating APIs, you’ll often deal with operations that don’t finish instantly, such as fetching data or reading files.

Share this article ...

💬WhatsApp📘Facebook💼LinkedIn🐦X