DEV Community

Cover image for Understanding JavaScript Classes Objects and the ‘new’ Keyword: A Beginner’s Guide
Nabin Kandel
Nabin Kandel

Posted on

Understanding JavaScript Classes Objects and the ‘new’ Keyword: A Beginner’s Guide

JavaScript Classes & the new Keyword: A Beginner's Guide to OOP 🎯

If you're just starting with JavaScript and Object-Oriented Programming (OOP), the class and new keywords might seem confusing. Let me break it down in the simplest way possible.


📐 What is a Class?

Think of a class as a blueprint or a recipe. It doesn't contain actual data — it just describes what kind of data an object should have.

class Student {
  name;
  age;
  gender;
}
Enter fullscreen mode Exit fullscreen mode

This Student class is like saying: "Every student should have a name, age, and gender."

But we haven't created any actual students yet—just the plan.


🛠️ Creating Objects from a Class

To create actual students (objects) from our blueprint, we use the new keyword:

const ram = new Student();
ram.name = "Ram";
ram.age = 10;
ram.gender = "Male";

const sita = new Student();
sita.name = "Sita";
sita.age = 9;
sita.gender = "Female";

const hari = new Student();
hari.name = "Hari";
hari.age = 11;
hari.gender = "Male";
Enter fullscreen mode Exit fullscreen mode

✅ Now we have three real student objects, each with their own data.


🔍 Using the Data

Once created, you can access each student's information:

console.log(ram.name);    // Output: Ram
console.log(sita.age);    // Output: 9
console.log(hari.gender); // Output: Male
Enter fullscreen mode Exit fullscreen mode

❓ Why Do We Need the new Keyword?

This is where most beginners get confused.

Why can't we just write const ram = Student()?

Without new

const ram = Student();  // ❌ Error! This won't work
Enter fullscreen mode Exit fullscreen mode

JavaScript doesn't understand that you want to create an object. It thinks you're trying to call a regular function.

With new

const ram = new Student();  // ✅ This works!
Enter fullscreen mode Exit fullscreen mode

The new keyword tells JavaScript:

"I want to create a brand new object based on this class."


⚙️ What Does new Actually Do?

When you use new Student(), JavaScript performs three important steps:

  1. 📦 Creates an empty object
  2. 🔗 Links it to the Student class (so it knows what properties/methods it should have)
  3. Returns the new object (and stores it in your variable)

🍪 A Real-World Analogy

Concept Real-World Equivalent
Class Cookie cutter 🍪
new Pressing the cutter into dough
Object The actual cookie you can eat

You can use the same cookie cutter (class) to make many cookies (objects), but each cookie is separate and can have different decorations (data).


📋 Quick Reference

Concept What It Is Example
Class Blueprint/Template class Student { }
Object Actual instance with data const ram = new Student()
new Keyword to create objects new Student()

🔑 Key Takeaways

  • One class can create many objects
  • Each object has its own independent data
  • The new keyword is required to create objects from a class

💻 Complete Example

Here's everything together:

// 1️⃣ Define the blueprint
class Student {
  name;
  age;
  gender;
}

// 2️⃣ Create objects using 'new'
const ram = new Student();
ram.name = "Ram";
ram.age = 10;
ram.gender = "Male";

const sita = new Student();
sita.name = "Sita";
sita.age = 9;
sita.gender = "Female";

// 3️⃣ Use the data
console.log(ram.name);   // Ram
console.log(sita.age);   // 9

// 4️⃣ Each object is independent
ram.age = 11;            // Only changes ram's age
console.log(ram.age);    // 11
console.log(sita.age);   // Still 9 👍
Enter fullscreen mode Exit fullscreen mode

Tags: #JavaScript #WebDevelopment #Programming #OOP #Beginners #LearnToCode

Top comments (0)