Classes & Objects

Preview — 3 of 10 questions

Which keyword is used to define a class in JavaScript?

javascript
class Person {
  constructor(name) {
    this.name = name;
  }
}
Afunction
Bclass
Cobject
Dconstructor

How do you create an object from a class?

javascript
class Car {
  constructor(brand) {
    this.brand = brand;
  }
}

let myCar = ______;
Anew Car("Toyota");
BCar.new("Toyota");
Ccreate Car("Toyota");
DCar("Toyota");

What is the purpose of the constructor method in a class?

javascript
class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
}

const p = new Person("Alice", 30);
console.log(p.name); // "Alice"
console.log(p.age);  // 30
ATo define static properties
BTo initialize object properties
CTo call parent class methods
DTo create private methods

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.