Categories
JavaScript Best Practices

Better JavaScript — JavaScript Versions and Numbers

Spread the love

Like any kind of apps, JavaScript apps also have to be written well.

Otherwise, we run into all kinds of issues later on.

In this article, we’ll look at ways to improve our JavaScript code.

Know Which JavaScript Version We Use

Knowing which JavaScript version we’re using is important.

This is because there’re always improvements with each version.

ES6 is the one version with the most changes.

It makes the code very different from older JavaScript if we use its features.

Also, we got to watch out for non-standard features which may crop up occasionally.

Browser support may be different for new features.

So we should watch out for the feature changes.

Also, API features may also change with different JavaScript engines.

So we want to make sure that the browsers we support have the support for the feature we want.

Otherwise, we need programs like Babel and polyfill libraries to make our app run.

Strict mode is another aspect of JavaScript we have to consider.

It forbids us from making mistakes like assigning to global values like undefined or create global variables accidentally.

It’s supposed to be backward compatible with older code, so we can apply strict mode in parts of the code with the 'use strict' directive.

So we can add:

"use strict";

to the top or we can write:

function foo(x) {
  "use strict";
  // ...
}

With strict mode on, we can write code like:

function foo(x) {
  "use strict";
  var arguments = [];
  // ...
}

Then we get the error ‘Uncaught SyntaxError: Unexpected eval or arguments in strict mode’ .

But if we need to concatenate a file with strict mode with ones that don’t, then we may have problems.

If we have strict mode enabled at the top on one file and another with strict mode enabled to a function, then strict mode is going to be applied everywhere.

To reduce the chance of problems, we should make our code out of the global scope.

So we can write:

file1.js

(function() {
  "use strict";

  function f() {
    // ...
  }
  //...

})()

and:

file2.js

(function() {
  function f() {
    // ...
  }
  //...

})()

file1.js has strict mode on and file2.js doesn’t.

So this is much better.

The functions and variables are also private which is another benefit.

JavaScript’s Floating-Point Numbers

JavaScript only has one kind of number.

They’re all floating-point.

So:

typeof 27;
typeof 9.6;
typeof -2.1;

all return 'number' .

JavaScript numbers are double-precision floating-point numbers.

They’re 64-bit encoding of numbers specified by the IEEE 754 standard.

Most arithmetic operators work with any kind of numbers.

So we can write:

0.1 * 1.9
-99 + 100;
21 - 12.3;

The bitwise operators only work with integers.

For instance, we write:

5 | 1;

and we get 5.

A simple expression can take multiple steps to evaluate.

If we convert a number to a binary string:

(5).toString(2);

toString has the radix 2 which means we convert to binary.

So decimal 5 is converted to binary digits.

it drops the extra 0 bits on the left since they don’t affect the value.

Bitwise operators work the same way.

Integers are converted to bits so they can be operated on.

Conclusion

We should check the JavaScript version we’re supporting so that our app can run.

Also, we should be aware that JavaScript only has one kind of number.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *