Skip to the content

Set and Map

JavaScript · Unit 8: More on arrays

new Set(array) creates a set: it keeps each value only once, in the order it arrived. size tells you how many it has.

To see what's inside, turn it into an array with [...s].

const s = new Set([3, 1, 3, 2, 1]);
console.log([...s]);
console.log(s.size);

Prints

[ 3, 1, 2 ]
3

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. Predict the output

    What does this code print?

  2. 2. Complete the code

    Complete it to print how many different sizes there are.

  3. 3. Predict the output

    What does this code print?

  4. 4. Complete the code

    Complete it to check whether the person is on the party list.

  5. 5. Predict the output

    What does this code print?

  6. 6. Find the bug

    This program should print how many lives Leo has, 3. Which line has the error?

  7. 7. Complete the code

    Complete it to count how many times you bought each thing.

  8. 8. Predict the output

    What does this code print?

  9. 9. Find the case that fails

    hasDuplicates(nums) should return true if any number shows up more than once, whether they're next to each other or not. Which call does it fail with?

  10. 10. Put the lines in order

    Order the function that returns the numbers without duplicates, from smallest to largest.

  11. 11. Predict the output

    How many different customers placed orders?

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 →