0
0
Javascriptprogramming~15 mins

Class syntax in Javascript - Mini Project: Build & Apply

Choose your learning style9 modes available
Class syntax
📖 Scenario: Imagine you are creating a simple program to keep track of pets in a pet store. Each pet has a name and an age.
🎯 Goal: You will create a class called Pet to represent each pet. Then you will create an object from this class and display its details.
📋 What You'll Learn
Create a class named Pet with a constructor that takes name and age as parameters
Inside the constructor, set the name and age properties
Create an object called myPet from the Pet class with the name Buddy and age 3
Print the pet's name and age using console.log
💡 Why This Matters
🌍 Real World
Classes help organize data and behavior for things like pets, users, or products in real apps.
💼 Career
Understanding classes is key for many programming jobs, especially in JavaScript frameworks and backend development.
Progress0 / 4 steps
1
Create the Pet class
Write a class named Pet with a constructor that takes name and age as parameters and sets them as properties.
Javascript
Need a hint?

Use the class keyword to create a class. Inside it, write a constructor method that sets this.name and this.age.

2
Create a Pet object
Create an object called myPet from the Pet class with the name Buddy and age 3.
Javascript
Need a hint?

Use new Pet("Buddy", 3) to create the object and assign it to myPet.

3
Access pet properties
Use console.log to print the name and age properties of myPet.
Javascript
Need a hint?

Use console.log(myPet.name) and console.log(myPet.age) to show the values.

4
Display the pet details
Print a message using console.log that says: "Buddy is 3 years old." using the properties of myPet and a template string.
Javascript
Need a hint?

Use a template string with backticks: `${myPet.name} is ${myPet.age} years old.`