A class is a blueprint for creating objects. It defines properties and methods that the created objects will have.
Click to reveal answer
beginner
How do you define a class in JavaScript?
Use the <code>class</code> keyword followed by the class name and curly braces. Inside, define a <code>constructor</code> method and other methods.
Click to reveal answer
beginner
What is the purpose of the constructor method in a class?
The constructor method runs automatically when you create a new object from the class. It sets up initial values for the object.
Click to reveal answer
beginner
How do you create an object from a class?
Use the <code>new</code> keyword followed by the class name and parentheses. For example: <code>const obj = new ClassName();</code>
Click to reveal answer
beginner
Can class methods access properties of the object? How?
Yes, class methods use <code>this</code> to access the object's properties and other methods.
Click to reveal answer
Which keyword is used to define a class in JavaScript?
Adefine
Bfunction
Cobject
Dclass
✗ Incorrect
The class keyword is used to define a class.
What does the constructor method do?
AInitializes new objects
BDeletes objects
CCreates a new class
DCalls other methods
✗ Incorrect
The constructor method initializes new objects when created.
How do you create an object from a class named Car?
Aconst car = new Car();
Bconst car = Car();
Cconst car = create Car();
Dconst car = class Car();
✗ Incorrect
Use new Car(); to create an object from the class.
Inside a class method, how do you refer to the current object?
Aself
Bcurrent
Cthis
Dobject
✗ Incorrect
The keyword this refers to the current object inside class methods.
Which of these is NOT true about JavaScript classes?
AClasses can have methods
BClasses are functions
CClasses can have constructors
DClasses are blueprints for objects
✗ Incorrect
Classes are special syntax, not regular functions, even though under the hood they use functions.
Explain how to create a class with a constructor and a method in JavaScript.
Think about the basic structure of a class and how you make objects from it.
You got /4 concepts.
Describe the role of the this keyword inside class methods.
Consider how methods know which object's data to use.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a class in JavaScript?
easy
A. To create blueprints for objects with shared properties and methods
B. To execute code immediately
C. To store data in a database
D. To style HTML elements
Solution
Step 1: Understand what a class represents
A class is a blueprint or template for creating objects that share properties and behaviors.
Step 2: Compare options with the definition
Only To create blueprints for objects with shared properties and methods correctly describes the purpose of a class in JavaScript.
Final Answer:
To create blueprints for objects with shared properties and methods -> Option A
Quick Check:
Class purpose = blueprint for objects [OK]
Hint: Classes define object templates, not immediate actions [OK]
Common Mistakes:
Confusing classes with functions that run immediately
Thinking classes store data like databases
Mixing classes with styling or HTML
2. Which of the following is the correct syntax to define a class named Car with a constructor?
easy
A. class Car() { constructor() { } }
B. function Car() { constructor() { } }
C. class Car { function constructor() { } }
D. class Car { constructor() { } }
Solution
Step 1: Recall correct class syntax
A class is declared with the keyword class, followed by the class name and curly braces. The constructor is a method inside without the function keyword.
Step 2: Check each option
Only class Car { constructor() { } } is correct. The other options have syntax errors such as parentheses after the class name, using function Car() declaration, or using the function keyword for the constructor.
Final Answer:
class Car { constructor() { } } -> Option D
Quick Check:
Class syntax = class Name { constructor() {} } [OK]
Hint: Class constructor has no 'function' keyword inside class [OK]
Common Mistakes:
Adding parentheses after class name
Using 'function' keyword inside class methods
Defining constructor outside class body
3. What will be the output of the following code?
class Person {
constructor(name) {
this.name = name;
}
greet() {
return `Hello, ${this.name}!`;
}
}
const p = new Person('Anna');
console.log(p.greet());
medium
A. Hello, Anna!
B. Hello, undefined!
C. Hello, !
D. Error: greet is not a function
Solution
Step 1: Understand object creation and constructor
The Person class constructor sets this.name to the passed argument. Here, 'Anna' is passed, so this.name becomes 'Anna'.
Step 2: Call the greet method
The greet method returns a string using this.name. Since this.name is 'Anna', the returned string is 'Hello, Anna!'.
Final Answer:
Hello, Anna! -> Option A
Quick Check:
Method uses this.name = 'Anna' [OK]
Hint: Constructor sets properties; methods use them with this [OK]
Common Mistakes:
Forgetting to pass argument to constructor
Expecting greet to print instead of return
Confusing this.name with undefined
4. Identify the error in the following class code:
class Animal {
constructor(type) {
this.type = type;
}
speak() {
console.log('I am a ' + type);
}
}
const a = new Animal('Dog');
a.speak();
medium
A. Incorrect class declaration syntax
B. Missing constructor keyword
C. Using undefined variable 'type' inside speak method
D. Cannot create instance with new keyword
Solution
Step 1: Check variable usage inside speak method
The method uses type directly, but type is not defined in its scope. It should use this.type to access the instance property.
Step 2: Confirm other parts are correct
The constructor and class syntax are correct, and creating an instance with new is valid.
Final Answer:
Using undefined variable 'type' inside speak method -> Option C
Quick Check:
Use this.type to access property inside methods [OK]
Hint: Use this.property inside class methods to access instance data [OK]
Common Mistakes:
Using variable name without this inside methods
Forgetting to use new keyword when creating instance
Misnaming constructor method
5. Given the class below, what will be the output of console.log(car.getDescription());?
class Vehicle {
constructor(make, model) {
this.make = make;
this.model = model;
}
getDescription() {
return `${this.make} ${this.model}`;
}
}
class Car extends Vehicle {
constructor(make, model, year) {
super(make, model);
this.year = year;
}
getDescription() {
return `${this.year} ${super.getDescription()}`;
}
}
const car = new Car('Toyota', 'Corolla', 2020);
console.log(car.getDescription());
hard
A. Toyota Corolla
B. 2020 Toyota Corolla
C. Corolla Toyota 2020
D. Error: super.getDescription is not a function
Solution
Step 1: Understand inheritance and super calls
The Car class extends Vehicle. The Car constructor calls super(make, model) to initialize those properties in the parent class.
Step 2: Analyze getDescription method in Car
The Car class overrides getDescription and calls super.getDescription() to get the parent class description, then prepends the year.
Step 3: Substitute values
super.getDescription() returns 'Toyota Corolla'. Adding this.year (2020) before it results in '2020 Toyota Corolla'.
Final Answer:
2020 Toyota Corolla -> Option B
Quick Check:
super.method() calls parent method [OK]
Hint: Use super.method() to call parent class methods inside subclass [OK]
Common Mistakes:
Forgetting to call super() in subclass constructor
Not using super.getDescription() inside overridden method
Mixing order of make and model in output
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.