Skip to the content

Numbers

JavaScript · Unit 17: What JavaScript brings along

Number() converts the whole text: if even one letter is left over, it gives NaN. parseInt() is more forgiving: it reads the digits at the start and stops where they run out.

console.log(Number("42px"));
console.log(parseInt("42px"));

Prints

NaN
42

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

    The weight is written as "18kg". Fill in the blank so the sum prints 20.

  3. 3. Predict the output

    What does this code print?

  4. 4. Multiple choice

    r holds the result of a calculation. How do you check whether it came out as NaN?

  5. 5. Predict the output

    What does this code print?

  6. 6. Find the bug

    With "hey" this program should print "That's not an age". Which line has the error?

  7. 7. Multiple choice

    Why does 0.1 + 0.2 === 0.3 give false?

  8. 8. Predict the output

    What does this code print?

  9. 9. Find the case that fails

    total(text) adds 50 of shipping to the price written in the text; if the text carries no number at all, the total is just the shipping. Which call breaks it?

  10. 10. Put the lines in order

    Put amount(text) in order: the number the text carries, or 0 if it carries none.

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 →