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 arrays and global variables in JavaScript.
Make Sure that All Array Indexes are Within the Bounds of the Array
We should make sure that we don’t access array indexes that are out of bounds.
Otherwise, we get undefined as the returned value.
Consider Using Objects Instead of Arrays
Arrays are meant to be accessed sequentially, so if we don’t need that, we may want to use an object instead.
This way, we won’t have to worry about indexes and just access things the way we want.
JavaScript also has maps for key-value pairs and sets for containers that don’t hold duplicate values.
Check the End Points of Arrays
We should check the endpoints of arrays to catch errors that may occur there.
Off by one error isn’t good, so we should be checking for those.
If an Array is Multidimensional, Make Sure its Subscripts are used in the Correct Order
arr[i][j]
and arr[j][i]
are completely different. So we should make sure that the index is in the right order.
Watch Out for Index Cross-Talk
If we have nested loops, it’s easy to confuse arr[j]
with arr[i]
, so we should make sure that our code don’t have typos like that or we’ll get unexpected results.
Inadvertent Changes to Global Data
It’s easy to mess up global data since they can be accessed anywhere, so we shouldn’t use them anywhere.
Instead, we can use local variables as much as possible.
Block-scoped variables are even better.
Code Reuse Hindered by Global Data
If we want to reuse code that has global data, then we got to make sure that the global data aren’t affected by the reuse.
This means that reusing code that references global data is hard.
This is another reason that we should have global data in our apps.
Reasons to Use Global Data
It’s rare that we have to have values that must be global, but they may exist.
Things like constants for the mode of our app may be a constant.
Global variables may also be used for simulating enum types if they don’t exist, which is the case in JavaScript.
If we have some values that are related and we need to access them everywhere, then we may want to make one or more global variables out of them.
Also, if we have data that are used everywhere, we may also want to define them as global data.
Use Global Data Only as a Last Resort
Global data should only be used as a last resort.
If something can’t be local at all, then they may have to be global.
However, that shouldn’t be the case in most cases with JavaScript modules.
Access Function
An access function allows us to access global variables in a controlled manner.
We may want to create functions for getter and setting global variables.
Also, we may want to have a locking mechanism for accessing global variables so that we won’t have issues with multiple pieces of code accessing the same global variable.
Reduce the Risks of Using Global Variables
If we ever declare global variables, we should make their name obvious.
For instance, all global variables may start with g
.
We should also create a list of all global variables so that everyone knows that they’re global.
They also shouldn’t contain intermediate results.
And we definitely shouldn’t put everything in one big global object and pass them everywhere.
That’s just a recipe for disaster since they can be changed by anything in our program.
Conclusion
We should avoid global variables as much as we can. They just aren’t good for much and they may cause lots of unexpected results.
If we want to use global variables, then we’ve to make sure that they’re accessed in an orderly manner.
If we’re using arrays, then we should watch out for off-by-one errors.
Also, we don’t have to use an array if we don’t need to access the data sequentially. We can use other things like sets and maps.