SA-21/06, C-11, Asapur, Varanasi, UP support@kamaldnp.com +91 8318013442
JavaScript
1. JavaScript Tutorial for Beginners — Learn JS from Scratch 2. Where to Put JavaScript Code — Head, Body or .js File 3. JavaScript Output Methods — alert, innerHTML, console.log 4. JavaScript Syntax Explained — Statements & Rules 5. JavaScript Comments — How to Write // and /* */ 6. JavaScript Variables — var, let, const Explained 7. JavaScript Operators — Arithmetic, Comparison, Logical 8. JavaScript Data Types — string, number, object, array 9. JavaScript Functions Tutorial — Parameters & Return Values 10. JavaScript Objects — Properties, Methods & this Keyword 11. JavaScript Scope — Global, Local & Block Scope Explained 12. JavaScript Events — addEventListener with Examples 13. JavaScript Strings — Methods, slice, replace & Templates 14. JavaScript Numbers — parseInt, toFixed, NaN Explained 15. JavaScript Arrays — Create, Read, push, pop & More 16. JavaScript Array Methods — map, filter, forEach, find 17. JavaScript Date Object — new Date(), getFullYear & More 18. JavaScript Math Object — random, round, floor, ceil 19. JavaScript if else — Conditions with Examples 20. JavaScript switch Statement — case, break, default 21. JavaScript for Loop — for, for...of, for...in Examples 22. JavaScript while Loop — while & do...while Explained 23. JavaScript Type Conversion — Number, String, Boolean 24. JavaScript try catch — Error Handling with finally 25. JavaScript JSON — stringify & parse with Examples 26. JavaScript Arrow Functions — => Syntax & this 27. JavaScript Classes — Constructor & Objects Explained 28. JavaScript Promises & async/await Explained Simply 29. JavaScript DOM Tutorial — Find & Change Page Elements 30. JavaScript Form Validation Tutorial with Examples 31. JavaScript Fetch API Tutorial — fetch() with Examples 32. JavaScript localStorage — setItem, getItem Explained 33. JavaScript Timers — setTimeout & setInterval Examples 34. JavaScript Regular Expressions — test() & match() Guide 35. JavaScript Debugging — console.log & Fixing Errors
Final Test — Certificate ₹10
Try this code live
Lesson 27 of 35

JavaScript Classes — Constructor & Objects Explained

4 min read · Free · No login needed

So far you have made objects one by one: { name: "Priya", marks: 92 }, then another, then another — copying the same shape every time. When you need ten students, that copying gets old. A class fixes it: you write the shape once, like a blueprint, and then stamp out as many objects as you need.

Diagram — see how it fits together
Diagram: JavaScript Classes

Think of a recipe. The recipe for masala chai is written once — tea, milk, ginger, sugar. Every cup you make follows the same recipe, but each cup is its own cup. A class is the recipe. Each object made from it is a cup. The recipe is called the class; the cup is called an instance.

Writing a class — class, constructor, methods

class Student {
  constructor(name, marks) {
    this.name = name;
    this.marks = marks;
  }
  result() {
    return this.marks >= 40 ? "Pass" : "Fail";
  }
}
let priya = new Student("Priya", 92);
console.log(priya.result()); // "Pass"

Three parts to learn. The class keyword names the blueprint. The constructor is a special method that runs when you create a new object with new — it receives the starting values and stores them on this. And the methods — like result() — are the things every object made from the class can do. Notice there are no commas between the methods, and no function keyword in front of them: inside a class, you just write the name.

Note: this inside a class method means "this object" — the particular student you are working with. priya.result() reads priya.marks; rahul.result() reads rahul.marks. Same method, different object, different answer.

Inheritance — extends and super

Sometimes one blueprint is almost like another. A Teacher has a name and a subject; a Student has a name and marks. Both have a name. Instead of writing the name part twice, one class can extend another:

class Person {
  constructor(name) { this.name = name; }
}
class Student extends Person {
  constructor(name, marks) {
    super(name);      // calls the parent's constructor
    this.marks = marks;
  }
}

extends says "this class builds on that one". super(name) calls the parent's constructor first — it must come before you use this in the child's constructor, or JavaScript throws an error. The child gets everything the parent has, plus its own extras.

Tip: Remember it like this: the class is the recipe, the object is the dish. Write the recipe once, cook as many dishes as you need. And super always goes first — the parent sets the table before the child sits down.

Common mistakes

  • Forgetting new — Student("Priya", 92) without new runs the constructor as a plain function and returns undefined. Always write new.
  • Using this before super() in a child constructor — JavaScript throws an error. Call super() first.
  • Putting commas between methods in a class — methods are separated by nothing, just like in an object literal without commas. A comma there is a syntax error.
  • Writing function before a method name inside a class — the keyword is not used there. Just write result() { ... }.

Remember

  • A class is a blueprint; an object made from it is an instance.
  • constructor runs when you write new and sets the starting values.
  • Methods inside a class have no function keyword and no commas between them.
  • extends builds one class on another; super() calls the parent's constructor first.
  • The class is the recipe, the object is the dish.

Example explained

  • class Student { constructor(name, marks) { ... } } — the blueprint: every student has a name and marks.
  • result() { return this.marks >= 33 ? "Pass" : "Fail"; } — a method every student object can use. The pass mark is 33.
  • let priya = new Student("Priya", 92); — stamps out one student object from the blueprint.
  • let rahul = new Student("Rahul", 28); — stamps out a second one. Same class, different values.
  • document.getElementById("out").textContent = priya.name + ": " + priya.result() + " — " + rahul.name + ": " + rahul.result(); — shows both results on the page: Priya passes, Rahul fails.

Example

Example
<!DOCTYPE html>
<html>
<head>
<title>Exam Results — Classes</title>
<style>
body { font-family: Verdana, sans-serif; background-color: #fff8e1; }
h1 { color: #b45309; text-align: center; }
.card { background-color: white; border: 2px solid #f59e0b; padding: 16px; width: 360px; margin: 20px auto; }
button { background-color: #b45309; color: white; border: none; padding: 10px 18px; font-size: 16px; }
#out { font-weight: bold; color: green; }
</style>
</head>
<body>

<h1>Exam Results</h1>
<div class="card">
<p><button onclick="showResults()">Show results</button></p>
<p id="out">Click the button to see Priya's and Rahul's results.</p>
<p>Priya scored 92, Rahul scored 28. The pass mark is 33.</p>
</div>

<script>
class Student {
  constructor(name, marks) {
    this.name = name;
    this.marks = marks;
  }
  result() {
    return this.marks >= 33 ? "Pass" : "Fail";
  }
}
function showResults() {
  let priya = new Student("Priya", 92);
  let rahul = new Student("Rahul", 28);
  document.getElementById("out").textContent =
    priya.name + ": " + priya.result() + " — " + rahul.name + ": " + rahul.result();
}
</script>

</body>
</html>

Output

Output — how it looks in the browser
Output of JavaScript Classes — Constructor & Objects Explained

Try it yourself — live editor

Change the code below and press Run to see the result instantly. No file to save, no login needed.

Result

Frequently asked questions

The questions learners ask most about this lesson — read the question, then open it to see the answer.

1. What is the difference between a class and an object?

A class is the blueprint written once, like a recipe; each object made from it with new is an instance, like a cup.

2. What does the constructor do?

It runs when you create a new object with new, receives the starting values, and stores them on this.

3. Do I write commas between class methods?

No. Inside a class you just write the method name, with no commas and no function keyword in front.

Why learn this yourself — and not just ask AI?

AI can write code for you, but it cannot think for you. When you learn HTML yourself, your brain builds the real skill: you understand why a page looks the way it does, you can fix it when it breaks, and you can answer it in an exam or an interview without help. That is the skill that gets you a job — not a copied answer.

Get certified — just ₹10

Finish all 35 lessons, pass the test (80%), pay ₹10 and get your certificate.

Take the test

Prefer watching? Video courses teach the same topics step by step.

Go further with a full course

Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.

AI Prompt Engineering Webinar 2026
View
Master Python + AI Skills in 30 Days
₹499
View
Full Stack PHP Development Course 2026
₹3,000
View
Browse all courses
JavaScript — Lessons
1. JavaScript Tutorial for Beginners — Learn JS from Scratch 2. Where to Put JavaScript Code — Head, Body or .js File 3. JavaScript Output Methods — alert, innerHTML, console.log 4. JavaScript Syntax Explained — Statements & Rules 5. JavaScript Comments — How to Write // and /* */ 6. JavaScript Variables — var, let, const Explained 7. JavaScript Operators — Arithmetic, Comparison, Logical 8. JavaScript Data Types — string, number, object, array 9. JavaScript Functions Tutorial — Parameters & Return Values 10. JavaScript Objects — Properties, Methods & this Keyword 11. JavaScript Scope — Global, Local & Block Scope Explained 12. JavaScript Events — addEventListener with Examples 13. JavaScript Strings — Methods, slice, replace & Templates 14. JavaScript Numbers — parseInt, toFixed, NaN Explained 15. JavaScript Arrays — Create, Read, push, pop & More 16. JavaScript Array Methods — map, filter, forEach, find 17. JavaScript Date Object — new Date(), getFullYear & More 18. JavaScript Math Object — random, round, floor, ceil 19. JavaScript if else — Conditions with Examples 20. JavaScript switch Statement — case, break, default 21. JavaScript for Loop — for, for...of, for...in Examples 22. JavaScript while Loop — while & do...while Explained 23. JavaScript Type Conversion — Number, String, Boolean 24. JavaScript try catch — Error Handling with finally 25. JavaScript JSON — stringify & parse with Examples 26. JavaScript Arrow Functions — => Syntax & this 27. JavaScript Classes — Constructor & Objects Explained 28. JavaScript Promises & async/await Explained Simply 29. JavaScript DOM Tutorial — Find & Change Page Elements 30. JavaScript Form Validation Tutorial with Examples 31. JavaScript Fetch API Tutorial — fetch() with Examples 32. JavaScript localStorage — setItem, getItem Explained 33. JavaScript Timers — setTimeout & setInterval Examples 34. JavaScript Regular Expressions — test() & match() Guide 35. JavaScript Debugging — console.log & Fixing Errors
Final Test — Certificate ₹10

🤖 KAMALDNP AI Assistant

Ask about courses, website & more

Hi! I'm KAMALDNP AI Assistant. Ask me about our courses, website features, or anything else! 😊
Please wait 5s...
🤖 KAMALDNP Assistant ✕

Request human callback:

🔐 Login Required
✕

Please log in to download. You will stay on this page after logging in!

New here? Register now →