JavaScript Numbers for Pythonistas (Summary)

The roastery’s batch planner now runs in JavaScript. For tonight’s paste of "37.5", batch-plan.js prints the same four lines as batch_plan.py. The two scripts also agree on the blank paste and "1_200".

You can now:

  • Use Number for the planner’s ordinary whole and decimal values, and ask Number.isInteger() whether the stored value is whole
  • Guard against division by zero, which raises an error in Python but gives NaN, Infinity, or -Infinity in JavaScript
  • Use Math.floor(a / b) for the planner’s // calculation and Math.trunc() for int() on finite numbers, pairing Math.floor() with ((a % n) + n) % n and Math.trunc() with %
  • Round with Math.ceil() and Math.round(x * 100) / 100
  • Convert text with Number(), treat blank text as missing, and check for NaN with Number.isNaN()
  • Read a number off the front of text such as "17.5kg" with parseFloat()
  • Format numbers last with .toFixed() and Intl.NumberFormat, naming the locale

Most of that came from knowing which Python habits to keep and which to drop.

Keep and Drop Your Python Habits

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

Become a Member to join the conversation.