0
0
Javascriptprogramming~20 mins

Instance methods in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Instance Methods Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of instance method call
What is the output of this JavaScript code?
Javascript
class Car {
  constructor(make) {
    this.make = make;
  }
  getMake() {
    return `This car is a ${this.make}`;
  }
}
const myCar = new Car('Toyota');
console.log(myCar.getMake());
Aundefined
B"This car is a myCar"
C"This car is a Toyota"
DTypeError
Attempts:
2 left
💡 Hint
Remember that instance methods use 'this' to access properties of the object.
Predict Output
intermediate
2:00remaining
Value of property after method call
What is the value of 'counter.count' after running this code?
Javascript
class Counter {
  constructor() {
    this.count = 0;
  }
  increment() {
    this.count += 1;
  }
}
const counter = new Counter();
counter.increment();
counter.increment();
A0
BNaN
Cundefined
D2
Attempts:
2 left
💡 Hint
Each call to increment adds 1 to the count property.
Predict Output
advanced
2:00remaining
Output with method using arrow function
What is the output of this code?
Javascript
class Person {
  constructor(name) {
    this.name = name;
  }
  greet = () => {
    return `Hello, my name is ${this.name}`;
  }
}
const p = new Person('Alice');
console.log(p.greet());
A"Hello, my name is undefined"
B"Hello, my name is Alice"
CSyntaxError
DTypeError
Attempts:
2 left
💡 Hint
Arrow functions keep the 'this' from their surrounding context.
Predict Output
advanced
2:00remaining
Output when method is extracted from instance
What is the output of this code?
Javascript
class Dog {
  constructor(name) {
    this.name = name;
  }
  bark() {
    return `${this.name} says woof!`;
  }
}
const dog = new Dog('Buddy');
const barkFn = dog.bark;
console.log(barkFn());
A"undefined says woof!"
B"Buddy says woof!"
CTypeError
DReferenceError
Attempts:
2 left
💡 Hint
When a method is called without its object, 'this' is undefined in strict mode.
🧠 Conceptual
expert
2:00remaining
Why use instance methods instead of static methods?
Which is the best reason to use instance methods instead of static methods in a class?
AInstance methods can access and modify individual object data, while static methods cannot.
BInstance methods run faster than static methods.
CStatic methods cannot be called from outside the class.
DStatic methods automatically bind 'this' to the instance.
Attempts:
2 left
💡 Hint
Think about what data each method type can access.