Skip to the content

Values that aren't there

JavaScript · Unit 9: Errors and exceptions

undefined is what JavaScript puts when nobody gave a value: an empty variable, a property that doesn't exist, a function with no return.

null is what you put, on purpose, to say "there's nothing here".

let winner;
console.log(winner);
let prize = null;
console.log(prize);

Prints

undefined
null

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. Multiple choice

    You're saving a reservation and no table has been assigned yet. What value do you give table?

  3. 3. Predict the output

    What does this code print?

  4. 4. Predict the output

    What does this code print?

  5. 5. Complete the code

    Complete it to greet by name, or as "friend" if no name arrives.

  6. 6. Predict the output

    How many values get into the if?

  7. 7. Find the bug

    If someone scored 0 goals, it should say 0; "no data" is only for when it's missing. With Leo it should print "Leo: 0". Which line has the error?

  8. 8. Find the case that fails

    toPay(p) should subtract the item's discount, or 10 if it has no off. A discount of 0 is valid. Which call does it fail with?

  9. 9. Multiple choice

    Which of these lines prints 10?

  10. 10. Predict the output

    You put ?. and ?? together. 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 →