Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Reading a Number Off the Front of Text

Reading a Number Off the Front of Text

In this lesson, you ported the total of the web shop’s export, where each café’s line carries its unit, and met JavaScript’s two parse functions for text that starts with a number.

  • The export lists tonight’s orders as "12kg", "8kg" and "17.5kg", which add up to 37.5 kg. Python strips the unit with .removesuffix("kg") and converts the rest with float().
  • JavaScript strings have no .removesuffix(), and Number("12kg") is NaN, because Number() wants the whole text to be a number.
  • parseInt() reads digits from the front of the text and ignores the rest, so parseInt("12kg") is 12. It looks like Python’s int(), but it stops at the first character that can’t be part of a whole number, so parseInt("17.5kg") is 17, with no error.
  • Python’s int() refuses the same number, even from the cleaned line: int("17.5") raises ValueError: invalid literal for int() with base 10: '17.5'.
  • Number() and parseInt() aren’t two spellings of one conversion. On "12kg", Number() gives NaN and parseInt() gives 12. On "17.5", Number() gives 17.5 and parseInt() gives 17.
  • parseFloat() reads the decimal point and the digits after it, and stops at the first character that can’t be part of a number: parseFloat("17.5kg") is 17.5.
  • Unlike Python’s float(), which raises ValueError on "17.5kg", parseFloat() drops the unit without a word.
  • When text holds a whole number, such as a bag size of "250g", give parseInt() its radix, the number base, as Python’s int() can take: parseInt("250g", 10) gives 250, as int("250g".removesuffix("g"), 10) does in Python. The radix doesn’t make it strict: it still ignores the rest of the text.
  • Without a radix, parseInt() works out the base from the text: 0x marks base 16, so parseInt("0x1A") is 26, where Python’s int("0x1A") raises ValueError. Always give the radix, so the base is the one you meant, not guessed from the text: parseInt("0x1A", 10) is 0, still a wrong number and still no error.
  • Watch out: to drop the decimals from a number, as Python’s int() does, use Math.trunc(), not parseInt(). parseInt() turns a number into text first, and 0.0000005 becomes "5e-7", so parseInt(0.0000005) is 5, where Math.trunc() and Python’s int() give 0.
  • Use parseFloat(), or parseInt(text, 10), for text that starts with a number and goes on with a unit you expect: they ignore the rest silently. To check that a paste is only a number, use Number() and Number.isNaN(), after checking it isn’t blank.

Here’s the port as first written, with parseInt():

Language: JavaScript
const EXPORT_LINES = ["12kg", "8kg", "17.5kg"];

let totalKg = 0;
for (const line of EXPORT_LINES) {
  totalKg += parseInt(line);
}
console.log(`Export total: ${totalKg} kg`);

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:02 In the last lesson, Number() converted the pasted order, and Number.isNaN() caught a bad paste. In this lesson, you’ll read numbers out of text that holds more than a number.

00:15 The web shop’s export lists tonight’s three café orders, each with its unit. Together they add up to the order’s 37.5 kilos. The Python script loops over the lines.

00:28 It strips the "kg" with removesuffix(), converts the rest with float(), and adds it to the total. It prints 37.5 kilos.

00:40 In the JavaScript port, the lines, the total and the loop carry over with const, let, and for...of. The conversion doesn’t.

00:50 JavaScript strings have no removesuffix(), and Number("12kg") gives NaN, because Number() wants the whole text to be a number.

01:00 The first two lines are whole kilos, so you search for text to a whole number and find parseInt(), which sounds like Python’s int() for text.

01:09 It parses digits off the front and ignores the rest, so the first line gives 12. The port adds parseInt(line) to the total.

01:20 The third line isn’t a whole number. What will JavaScript print when you run the file? Your turn: pause the video and predict.

01:35 A total of 37 kilos, half a kilo short. There’s no error and no warning. Northside Kitchen’s 17.5 kilos became 17.

01:49 parseInt() reads as far as it can, and stops at the first character that can’t be part of a whole number. In "17.5kg", that’s the decimal point, so it stops at 17 and drops the rest.

02:03 Python’s int() would have refused that line. With int() in place of float(), the script stops with a ValueError and names the bad text.

02:13 In JavaScript, nothing stops, and the total is quietly wrong.

02:20 Side by side, Number() and parseInt() aren’t two spellings of one conversion: they disagree on both kinds of text. On "12kg", Number() gives NaN, and parseInt() gives 12.

02:34 On the third line without its unit, "17.5", Number() keeps 17.5, and parseInt() drops the decimals. So choose by what should happen to the rest of the text.

02:48 For decimals, parseFloat() parses the number. It reads the decimal point and the digits after it, and stops at the first character that can’t be part of a number.

02:59 Unlike Python’s float(), which refuses "17.5kg", it drops the unit without a word. It fills a new column in the table.

03:10 The fix swaps parseFloat() into the loop. Run the file again, and it prints 37.5 kilos, the same as Python.

03:21 For a whole number, like the bag size "250g" from the label settings, parseInt() is right. Give it a second argument, 10: the radix, or number base, as Python’s int() can take.

03:34 It still ignores the rest of the text. Without a radix, parseInt() guesses the base from the text. A leading "0x" marks base 16, so 1A is 26.

03:49 Python’s int() reads base 10 unless you ask, so it refuses that text. Always give parseInt() 10, so the base is the one you meant, not a guess.

04:00 It stops at the x and gives 0: still wrong, with no error.

04:07 Watch out, though: to drop the decimals from a number, as Python’s int() does, Math.trunc(), not parseInt(), is the one to use.

04:16 parseInt() turns a number into text first, and a tiny one such as 0.0000005 becomes "5e-7", so it reads a 5, where Math.trunc() and int() give 0.

04:30 For text that starts with a number and a unit you expect, use parseFloat(), or parseInt() with its 10: they ignore the rest silently.

04:40 To check that a paste is only a number, use Number() and Number.isNaN(), with the blank check from before.

04:49 Next up is the exercise Read the Evening’s Order. You’ll read a few of the head roaster’s pastes the way Python’s float() would, then total an export with lines like "17.5kg" and "1,200kg" in it.

Become a Member to join the conversation.