Skip to the content

Your first class

JavaScript · Unit 12: Classes and objects

A class is a mold for making objects of the same kind. class Pet {} creates the mold, and new Pet() creates a new object from that mold.

That object is like the ones you already know: you give it properties with a dot.

class Pet {}
const luna = new Pet();
luna.name = "Luna";
console.log(luna.name);

Prints

Luna

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Multiple choice

    class Pet {} already exists. Which line creates an object from that mold?

  2. 2. Complete the code

    Fill in the blank to create a book with the Book class.

  3. 3. Predict the output

    What does this code print?

  4. 4. Predict the output

    What does this code print?

  5. 5. Predict the output

    There's only one new here. What does it print?

  6. 6. Multiple choice

    A school program has a Student class and two objects, mia and leo. Which sentence is right?

  7. 7. Find the bug

    This program should print "Coco". Which line has the bug?

  8. 8. Put the lines in order

    Put the lines in order to create a ticket to Boston and print its city.

  9. 9. Predict the output

    One ticket is made per city. What does it print?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →