DEV Community

Narasimma Radhakrishnan
Narasimma Radhakrishnan

Posted on

JavaScript Objects & Constructors for Beginners 🚀

When I started learning JavaScript, objects and constructors were confusing at first. But once I understood how they work together, they became much easier.

What is an Object?

An object stores related information using key-value pairs.

let student = {
name: "Narasimma",
age: 21,
course: "B.Tech IT"
};

console.log(student.name);
console.log(student.age);

Output:

Narasimma
21

Think of an object like a student profile 📦.

What is a Constructor?

A constructor is used to create multiple objects with the same structure.

Instead of creating every student object manually, we can create a constructor:

function Student(name, age, course) {
this.name = name;
this.age = age;
this.course = course;
}

Now we can create multiple students:

let student1 = new Student("Narasimma", 21, "B.Tech IT");

let student2 = new Student("Arun", 22, "B.Tech CSE");

console.log(student1.name);
console.log(student2.name);

Output:

Narasimma
Arun
How Does new Work?

When we write:

let student1 = new Student("Narasimma", 21, "B.Tech IT");

JavaScript creates a new object and assigns the values to it.

Easy Way to Remember 💡

Object = One student 📦

Constructor = Student object factory 🏭

If you need 100 student objects, you don't have to write 100 objects manually. Create one constructor and reuse it.

Conclusion

Objects are one of the most important concepts in JavaScript. Once you understand objects, constructors, and the new keyword, you are ready to explore more advanced JavaScript concepts.

Learn the basics → Practice with small programs → Build projects. 🚀

javascript #webdev #beginners #coding

Top comments (1)

Collapse
 
nilark profile image
Nila •

Great explanation! The way you compared an object to a student profile and a constructor to an object factory makes the concept very easy for beginners to understand. The simple examples with new also make the topic much clearer.