JavaScript Best Practices — Dealing with Numbers

John Au-Yeung
May 15, 2020 · 4 min read
Photo by Austin Distel on Unsplash

JavaScript is a very forgiving language. It’s easy to write code that runs but has issues in it.

In this article, we’ll look at how we should work with numbers in JavaScript.

Avoid Magic Numbers

Magic numbers are numbers that are used everywhere but their meaning isn’t explicitly specified.

To make them easier to understand and change, we should assign them to a named constant so that we can see what they mean.

Also, we only have to change one place since the constant name is referenced instead of numeric literals.

So instead of writing:

We write:

Use Hard-Coded 0s and 1s if we Need to

0’s and 1’s are OK for hard coding since they’re used for incrementing or setting as an initial number.

We often increment numbers by 1, so we can hard code 1.

0 are used for initial values of variables, so that’s also OK to hard code.

Other numbers should be replaced with something more descriptive.

Anticipate Divide-by-Zero Errors

We shouldn’t expect that the denominator a fraction will never be 0. Therefore, when we divide a number by another number, we should make sure that the divisor is never 0.

Make Type Conversions Obvious

In JavaScript, types may be converted explicitly.

This may cause unexpected results.

To make them obvious, we should convert the variables explicitly.

So, we should use functions like Number , Number.parseInt , or Number.parseFloat to convert non-numbers to numbers.

The + sign before a variable or a non-number can also convert them to numbers.

Also, when we compare things that may be numbers of equality, we should use === so that we’ve to convert the operands to numbers with those functions and operators.

Avoid Mixed-Type Comparisons

Mixed type comparisons often create unexpected results.

Therefore, we should avoid them as much as possible. We should use the === operator to compare for equality.

If we’re checking for inequality, then we should convert both operands to the same type before comparing them.

So we should convert them both to numbers with those methods and operations we looked at before.

Check for Integer Division

We should check that the result of any integer division is what we expect.

We should make sure that division is done last if we have an expression with multiple arithmetic operations.

For instance, instead of writing:

we write:

to avoid anything unexpected.

Check for Integer Overflow

We should check if our number’s absolute value is bigger than Number.MAX_SAFE_INTEGER is we’re working with large integers.

If it is, then we need to use the bigInt type instead.

bigInt is a primitive data type that can let us store integers with magnitude bigger than Number.MAX_SAFE_INTEGER .

We can do arithmetic with other bigInts only and not numbers.

Photo by Austin Distel on Unsplash

Avoid Additions and Subtractions on Numbers that have Greatly Different Magnitudes

The result might not be what we expect if we do additions and subtractions on numbers that have big difference in magnitudes.

For instance, 1000000000.00 + 0.00000000001 returns 1000000000 since there aren't enough significant digits to include all the digits of the result.

Avoid Equality Comparisons

Equality comparisons don’t work well with numbers.

For instance if we have:

We get 0.9999999999999999 as the value of sum instead of 0.1

Therefore, comparing both directly won’t work very well.

We may want to write our function to see if one number is close enough to what we want to be considered equal.

Anticipate Rounding Errors

As e can see, rounding errors happen frequently. If we add a number 10 times, it doesn’t equal to the number we expect, for example.

Conclusion

When we deal with numbers in JavaScript, we should keep a few things in mind.

Rounding errors are a problem that w should be aware of.

When we apply arithmetic operations to numbers, we don’t always get the same thing as we do the same thing on paper.

Therefore, we should be careful when checking for values.

Mixed-type comparisons are also bad. We should convert everything to numbers before comparing them.

The Startup

Get smarter at building your thing. Join The Startup’s +794K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

John Au-Yeung

Written by

Web developer. Subscribe to my email list now at https://thewebdev.info/subscribe/. Email me at hohanga@gmail.com

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +794K followers.

John Au-Yeung

Written by

Web developer. Subscribe to my email list now at https://thewebdev.info/subscribe/. Email me at hohanga@gmail.com

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +794K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store