DEV Community is a community of 756,528 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

John Au-Yeung

Posted on • Originally published at thewebdev.info on

Maintainable JavaScript — Numbers and Null

Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62

Subscribe to my email list now at http://jauyeung.net/subscribe/

Creating maintainable JavaScript code is important if want to keep using the code.

In this article, we’ll look at the basics of creating maintainable JavaScript code with some conventions for numbers and null.

Numbers

There’s only one kind of number in JavaScript.

Integers and floats are stored with the same data type.

There’re various kinds of number literals that we can write.

For instance, we can write:

const count = 10;

to write an integer.

To write decimals, we can write:

let price = 10.0;
var quantity = 10.00;

We can have decimals after the number.

However, we can also write:

const count = 10.;

But this is confusing so we should avoid it.

The hanging decimal is also useless.

We can also have a leading decimal point with our JavaScript numbers:

var price = .2;

But it’s clearer to just put the 0 before the decimal point:

var price = 0.2;

We should never write octal literals since they’re confusing and are deprecated:

var num = 010;

The 0 cause confusion between octal and decimal numbers.

We can also write JavaScript hex numbers:

let num = 0xFF;

to write numbers in scientific notation, we can use the letter e:

var num = 1e20;

1e20 is 100000000000000000000 or 10 ** 20 .

The hanging and leading decimals can easily be confused for mistakes.

They‘re forbidden in many style guides and can be caught with ESLint, JSLint, and JSHint.

Warnings will also be made if octal literals are encountered.

Null

null is often misunderstood and confused with undefined .

We should use undefined most of the time to reduce confusion.

But we can use null in a few cases.

We can use them to initialize a variable that may be assigned with an object later.

Also, we can use it compare against a variable that may be null .

And we can pass that into a function where an object is expected.

We can also return null in place of an object when there’s nothing to return.

But we shouldn’t use null to test whether an argument is supplied.

And we don’t test uninitialized variables for null .

So we can write:

let person = null;

Or we can write:

function createPerson() {
if (condition) {
return new Person("nick");
} else {
return null;
}
}

But we shouldn’t use it compare against an uninitialized variable like:

if (person != null) {
doWork();
}

We also shouldn’t check against null to see if a variable is passed in:`

`
function doWork(arg1, arg2, arg3, arg4) {
if (arg4 != null) {
doSomething();
}
}

`

We used != which is bad since it does automatic data type coercion and we check against null when we should be checking against undefined .

This is because if we don’t pass in an argument, then the parameter will be undefined .

null is a placeholder for objects.

It’s not a value to represent nothing.

Conclusion

We should be careful with placing decimal places with numbers.

Also, we shouldn’t use octal literals.

null should only be used in some limited cases.

The post Maintainable JavaScript — Numbers and Null appeared first on The Web Dev.