Bird
Raised Fist0
Javascriptprogramming~20 mins

Why classes are introduced in Javascript - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Why Classes Are Introduced
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use classes instead of functions?

In JavaScript, why were classes introduced when we already had functions?

ATo replace all functions with a new syntax that does not support inheritance.
BTo provide a clearer and simpler syntax for creating objects and handling inheritance.
CTo make JavaScript run faster by removing prototypes.
DTo prevent the use of objects and only allow primitive data types.
Attempts:
2 left
💡 Hint

Think about how classes help organize code and reuse features.

❓ Predict Output
intermediate
2:00remaining
Output of class vs function object creation

What is the output of this code?

Javascript
function Person(name) {
  this.name = name;
}
Person.prototype.greet = function() {
  return `Hello, ${this.name}`;
};

class Animal {
  constructor(name) {
    this.name = name;
  }
  greet() {
    return `Hi, ${this.name}`;
  }
}

const p = new Person('Alice');
const a = new Animal('Dog');
console.log(p.greet());
console.log(a.greet());
A
Hello, Alice
Hello, Dog
B
Hi, Alice
Hello, Dog
C
Hello, Alice
Hi, Dog
D
Hi, Alice
Hi, Dog
Attempts:
2 left
💡 Hint

Look at how greet() is defined in each case.

❓ Predict Output
advanced
2:00remaining
What happens with inheritance in classes?

What is the output of this code?

Javascript
class Vehicle {
  constructor(type) {
    this.type = type;
  }
  describe() {
    return `This is a ${this.type}`;
  }
}

class Car extends Vehicle {
  constructor(type, brand) {
    super(type);
    this.brand = brand;
  }
  describe() {
    return `${super.describe()} of brand ${this.brand}`;
  }
}

const myCar = new Car('car', 'Toyota');
console.log(myCar.describe());
ATypeError: super.describe is not a function
BThis is a Vehicle of brand Toyota
CThis is a car
DThis is a car of brand Toyota
Attempts:
2 left
💡 Hint

Check how super is used to call the parent method.

🧠 Conceptual
advanced
2:00remaining
Why classes improve code organization?

How do classes help organize code better compared to using only functions and objects?

AClasses group data and behavior together, making code easier to understand and reuse.
BClasses force all code to be written in one place, reducing modularity.
CClasses remove the need for variables and constants in code.
DClasses prevent the use of inheritance and polymorphism.
Attempts:
2 left
💡 Hint

Think about how classes bundle properties and methods.

❓ Predict Output
expert
2:00remaining
Output of class with private fields and methods

What is the output of this code?

Javascript
class Counter {
  #count = 0;
  increment() {
    this.#count++;
  }
  getCount() {
    return this.#count;
  }
}

const c = new Counter();
c.increment();
c.increment();
console.log(c.getCount());
console.log(c.#count);
A
2
TypeError
B
2
undefined
C
2
0
D
2
SyntaxError
Attempts:
2 left
💡 Hint

Private fields cannot be accessed outside the class.

Practice

(1/5)
1. Why were classes introduced in JavaScript?
easy
A. To remove the need for variables
B. To replace all functions with a new syntax
C. To make JavaScript run faster
D. To group data and related actions together in one place

Solution

  1. Step 1: Understand the purpose of classes

    Classes help organize data and functions that belong together, making code easier to manage.
  2. Step 2: Compare options with class purpose

    Only grouping data and actions matches the reason classes were introduced.
  3. Final Answer:

    To group data and related actions together in one place -> Option D
  4. Quick Check:

    Classes group data and actions = D [OK]
Hint: Classes bundle data and actions together [OK]
Common Mistakes:
  • Thinking classes make code run faster
  • Believing classes remove variables
  • Confusing classes with replacing all functions
2. Which of the following is the correct way to define a class in JavaScript?
easy
A. var Car = () => { }
B. function Car() { }
C. class Car { constructor() { } }
D. class Car() { }

Solution

  1. Step 1: Recall class syntax in JavaScript

    The correct syntax uses the keyword class followed by the class name and curly braces.
  2. Step 2: Check each option

    class Car { constructor() { } } uses correct syntax with class Car { constructor() { } }. class Car() { } has wrong parentheses after class name.
  3. Final Answer:

    class Car { constructor() { } } -> Option C
  4. Quick Check:

    Correct class syntax = A [OK]
Hint: Class syntax uses 'class Name { constructor() {} }' [OK]
Common Mistakes:
  • Adding parentheses after class name
  • Using function syntax instead of class
  • Confusing arrow functions with classes
3. What will be the output of this code?
class Dog {
  constructor(name) {
    this.name = name;
  }
  bark() {
    return `${this.name} says woof!`;
  }
}
const dog1 = new Dog('Buddy');
console.log(dog1.bark());
medium
A. "Buddy says woof!"
B. "dog1 says woof!"
C. undefined says woof!
D. Error: bark is not a function

Solution

  1. Step 1: Understand class and method usage

    The class Dog sets the name property via constructor and bark() returns a string using that name.
  2. Step 2: Trace the code execution

    dog1 is created with name 'Buddy'. Calling dog1.bark() returns 'Buddy says woof!'.
  3. Final Answer:

    "Buddy says woof!" -> Option A
  4. Quick Check:

    dog1.bark() = "Buddy says woof!" [OK]
Hint: Constructor sets name; bark() uses it in string [OK]
Common Mistakes:
  • Expecting dog1 as name instead of 'Buddy'
  • Thinking bark() is undefined
  • Confusing method call syntax
4. Find the error in this class definition:
class Person {
  constructor(name) {
    this.name = name;
  }
  greet() {
    console.log('Hello, ' + name);
  }
}
const p = new Person('Alice');
p.greet();
medium
A. Missing 'this' before 'name' inside greet() method
B. Constructor is missing parentheses
C. Class name should be lowercase
D. greet() method should return a string, not console.log

Solution

  1. Step 1: Check variable usage inside greet()

    The method uses name without this., so it looks for a local variable which does not exist.
  2. Step 2: Identify correct property access

    It should use this.name to access the instance property.
  3. Final Answer:

    Missing 'this' before 'name' inside greet() method -> Option A
  4. Quick Check:

    Use this.name to access property [OK]
Hint: Use 'this.' to access class properties inside methods [OK]
Common Mistakes:
  • Forgetting 'this' keyword
  • Thinking constructor syntax is wrong
  • Believing method must return instead of console.log
5. You want to create many similar objects representing books with title and author. Why is using a class better than creating separate objects manually?
hard
A. Classes make the code run faster than objects
B. Classes let you create many objects easily with shared structure and behavior
C. Classes automatically save objects to a database
D. Classes prevent any changes to the objects after creation

Solution

  1. Step 1: Understand the benefit of classes for similar objects

    Classes provide a blueprint to create many objects with the same properties and methods easily.
  2. Step 2: Compare options with class benefits

    Only Classes let you create many objects easily with shared structure and behavior correctly states that classes simplify creating many similar objects with shared structure and behavior.
  3. Final Answer:

    Classes let you create many objects easily with shared structure and behavior -> Option B
  4. Quick Check:

    Classes simplify creating many similar objects = A [OK]
Hint: Classes are blueprints for many similar objects [OK]
Common Mistakes:
  • Thinking classes speed up code execution
  • Believing classes save data automatically
  • Assuming classes make objects immutable
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard