Recommendations for Numbers in JavaScript

Question
What are some best practices for working with numbers in JavaScript?

Answer
You’ll want to avoid using the following formats:

// The hanging decimal point.
let price = 5.;

// The leading decimal point.
let price = .5;

// Octal or base 8 format.
// This represents 40, not 50.
let number = 050;

Comments
Add digits before and after decimal points to avoid confusion. Also, avoid using octal literals. Most developers aren’t familiar with them.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s