0
0
Javascriptprogramming~20 mins

Class syntax in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Class Syntax Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of method call in a class
What is the output of this code when calling obj.greet()?
Javascript
class Person {
  constructor(name) {
    this.name = name;
  }
  greet() {
    return `Hello, ${this.name}!`;
  }
}
const obj = new Person('Alice');
console.log(obj.greet());
ASyntaxError
B"Hello, undefined!"
CTypeError: obj.greet is not a function
D"Hello, Alice!"
Attempts:
2 left
💡 Hint
Remember how the constructor sets the name property and how methods access it with this.
Predict Output
intermediate
2:00remaining
Output of static method call
What will this code output when calling MathHelper.square(4)?
Javascript
class MathHelper {
  static square(x) {
    return x * x;
  }
}
console.log(MathHelper.square(4));
A16
BNaN
CTypeError: MathHelper.square is not a function
Dundefined
Attempts:
2 left
💡 Hint
Static methods are called on the class itself, not on instances.
Predict Output
advanced
2:00remaining
Output of inheritance and method override
What is the output of this code?
Javascript
class Animal {
  speak() {
    return 'Animal sound';
  }
}
class Dog extends Animal {
  speak() {
    return 'Woof!';
  }
}
const pet = new Dog();
console.log(pet.speak());
A"Woof!"
B"Animal sound"
CTypeError: pet.speak is not a function
Dundefined
Attempts:
2 left
💡 Hint
Child classes can replace methods from parent classes.
Predict Output
advanced
2:00remaining
Output of private field access
What happens when this code runs?
Javascript
class Counter {
  #count = 0;
  increment() {
    this.#count++;
  }
  getCount() {
    return this.#count;
  }
}
const c = new Counter();
c.increment();
c.increment();
console.log(c.getCount());
A0
BSyntaxError
C2
DTypeError: Cannot access private field
Attempts:
2 left
💡 Hint
Private fields start with # and are only accessible inside the class.
Predict Output
expert
3:00remaining
Output of method using super in subclass
What is the output of this code?
Javascript
class Vehicle {
  start() {
    return 'Starting vehicle';
  }
}
class Car extends Vehicle {
  start() {
    return super.start() + ' and car engine';
  }
}
const myCar = new Car();
console.log(myCar.start());
ATypeError: super.start is not a function
B"Starting vehicle and car engine"
Cundefined
D"Starting vehicle"
Attempts:
2 left
💡 Hint
super calls the method from the parent class.