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

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

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.

Google Tools for College Students to boost Productivity

Google Tools for College Students to boost Productivity

College life can feel like a roller coaster — assignments, exams, group projects, internships, and of course, trying to squeeze in a social life. The good news? You don’t have to do it all alone. Google has built a whole ecosystem of tools that can help students stay productive, organized, and even a little more stress-free. From taking notes to collaborating on projects, Google’s apps are like a digital toolkit every student should know about. Let’s explore how these tools can make your college journey smoother.

Google Pixel 9 Pro: The Next Big Thing in Smartphones?

Google Pixel 9 Pro: The Next Big Thing in Smartphones?

The smartphone world is buzzing again, and this time it’s all about the Google Pixel 9 Pro. According to recent leaks and reports, the upcoming Pixel lineup might just shake things up in a big way. Let’s dive into what makes the Pixel 9 Pro stand out and why everyone is talking about it.

Can AI Make Our Food Safer Than Ever?

Can AI Make Our Food Safer Than Ever?

Every year, millions of people around the world get sick from eating contaminated food. The World Health Organization (WHO) estimates that more than 600 million people fall ill annually, and about 4.2 million die from foodborne diseases. That’s a huge number — and the scary part is, much of it comes from something we can’t even see: toxic fungi known as mycotoxins. But now, scientists may have found a way to stop this problem before it even reaches our plates. Thanks to artificial intelligence (AI) and a powerful imaging technology, our food supply could soon become safer than ever.

What is Generative AI?

What is Generative AI?

Artificial Intelligence has gone from beating humans at chess to creating human-like poetry, realistic images, and even functional code. This creative side of AI is called Generative AI — and it’s one of the most exciting areas in modern technology. But to truly understand what Generative AI means, we need to take a quick trip through history, starting with an idea that shaped all of computer science: the Turing Machine.

The Evolution of AI - From Turing to Today

The Evolution of AI - From Turing to Today

Artificial Intelligence (AI) didn’t just appear out of thin air. The chatbot answering your questions today, the recommendation system suggesting your next Netflix binge, and the self-driving cars making headlines — all of these are the result of decades of innovation, setbacks, and breakthroughs. AI’s story is one of human curiosity: a mix of science fiction dreams, brilliant engineering, and a dash of “what if machines could think?” Let’s rewind the clock and see how it all began.

HTML the Easy Way-Unlock the web

HTML the Easy Way-Unlock the web

If you’ve ever wondered how websites are built, here’s the secret: almost every webpage you see starts with HTML. Whether it’s a simple blog post, an online shop, or a social media site – HTML is the foundation.

JUnit 5 and Mockito – From Scratch to Advanced

JUnit 5 and Mockito – From Scratch to Advanced

Learn JUnit 5 & Mockito from scratch to advanced with real-time Spring Boot examples. Covers unit & integration testing, annotations, MockMvc, H2 DB, and best practices.

Modern JavaScript for Developers in 2026

Modern JavaScript for Developers in 2026

Discover the modern JavaScript features developers should know in 2026 and learn how to future-proof your skills for the evolving web landscape. This in-depth guide covers JavaScript trends 2026, emerging frameworks, and best practices to help you stay ahead in web development. Explore the top JavaScript frameworks to watch in 2026, find out what’s new in modern JS features, and see how AI-assisted JavaScript development is shaping the future. Whether you’re updating your JavaScript developer roadmap 2026 or just getting started, this article will help you master the modern JavaScript tools and techniques needed to build fast, scalable, and future-ready applications.

Debouncing in JavaScript

Debouncing in JavaScript

In this article we will learn about the debouncing . We will learn about actual real problem and how to solve this problem using js debouncing.

Maven Tutorial

Maven Tutorial

Maven is a build automation tool primarily used for Java projects. It addresses two main aspects of building software: dependency management and project build lifecycle management.

Share this article ...

💬WhatsApp📘Facebook💼LinkedIn🐦X