Borrowing Techniques from Strongly Typed Languages in JS

Share this article

In this article we’ll discuss how you can use techniques from strongly typed languages in your JavaScript code. The techniques introduced will both reduce bugs in your code, and allow you to reduce the total amount of code you need to write. Although this article uses JavaScript as the example, you can also apply these techniques to most other languages with weak typing.

The JavaScript Type System

Let’s first do a quick recap on how the JavaScript data type system works. JavaScript splits its values into two categories:
  • Primitive types, such as String, Number and Boolean. When you assign a primitive type to a variable, you always create a new value which is a copy of the value you’re assigning.
  • Reference types, such as Object and Array. Assigning reference types always copies the same reference. To clarify this, let’s look at the following code example:
var a = [];
var b = a;

a.push('Hello');
The variable b will change when we change a, because they are both references to the same array. This is how all reference types work. JavaScript does not enforce types in any way, meaning that any variable can hold any data type at any point in time. The rest of this article will discuss the downsides of this, and how you can apply simple techniques from languages which do enforce types to write better JavaScript.

Introducing the Rule of Consistent Types

The rule of consistent types is simple in theory: all values should only have one type. Strongly typed languages enforce this on the compiler level, they will not let you mix and match types arbitrarily. Weak typing gives us a great amount of freedom. A common example of this is concatenating numbers into strings. You don’t need to do any tedious type casting like you would have to do, for example, in a language like C. Don’t worry, I won’t tell you to throw away all the convenience. The rule of consistent types only requires you to pay some attention to how your variables and functions behave, and as a result, your code will improve.

Types in Variables

First, let’s look at how the rule applies to variables. It’s very straightforward: your variables should always have one type only.
var text = 'Hello types';

// This is wrong! Don't do it!
text = 1;
The above example shows the problem. This rule requires us to pretend that the last line of code in this example will throw an error, because when we first defined the variable text, we gave it a value of type string and now we’re assigning a number to it. The rule of consistent types means we’re not allowed to change a variable’s type like that. It’s easier to reason about your code when your variables are consistent. It helps especially in longer functions, where it’s easy to lose sight of where the variables come from. I’ve accidentally caused bugs many times when working in codebases that didn’t respect this rule, because I saw a variable being declared, and then assumed it would keep the same type – because let’s face it, that makes sense doesn’t it? Usually there is no reason to assign a different type into the same variable.

Types in Function Parameters

The same rule applies here. The parameters for functions should also be consistent. An example of doing it wrong:
function sum(a, b) {
if (typeof a === 'string') {
a = 1;
}

return a + b;
}
What’s wrong with this? It’s generally considered bad practice to branch logic based on a type check. There are exceptions to this, but usually it would be a better option to use polymorphism. You should aim to make sure your function parameters also have only one type. It reduces the possibility of problems if you forget to account for the different types, and leads to simpler code because you don’t have to write code to handle all the different cases with types. A better way to write the sum function would be as follows:
function sum(a, b) {
return a + b;
}
Then, you handle the type check in the calling code instead of in the function. As you can see from the above, the function is now much simpler. Even if we have to move the type check to somewhere else, the earlier we can do them in our code, the better off we’ll be. We will discuss the use of type checking and typeof later in the article, including how type checks can easily cascade if used poorly.

Types in Function Return Values

This ties in with the two others: Your functions should always return values of the same type. We can take an example from AngularJS here. AngularJS provides a function to lowercase text, called angular.lowercase. There’s also a standard function for it, String.prototype.toLowerCase. We can compare their behavior to understand this part of the rule better:
var a = angular.lowercase('Hello Types');
var b = angular.lowercase(null);
The variable a will contain what you would expect: 'hello types'. However, what will b contain? Will it be an empty string? Will the function throw an exception? Or maybe it’s just going to be null? In this case, the value of b
is null. Notice how it was immediately difficult to guess what the result was going to be – we had three possible outcomes right off the bat. In the case of the Angular function, for non-string values, it will always return the input. Now, let’s see how the built-in one behaves:
var a = String.prototype.toLowerCase.call('Hello Types');
var b = String.prototype.toLowerCase.call(null);
The result of the first call is the same, but the second call throws an exception. The built-in function follows the rule of consistent types, and it does not allow incorrect parameter types. The returned value is also always a string. So we can say the built-in function is better, but you might be wondering how exactly? Let’s consider a typical use-case for a function like this. We’re using it at some point in our code to convert strings into lowercase. As is often the case in JavaScript code, we’re not 100% sure that our input is always going to be a string. It doesn’t matter, as because we’re good programmers, we’re assuming our code doesn’t have any bugs. What will happen if we’re using the function from AngularJS which doesn’t respect these rules? A non-string value goes through it without any problems. It might go through a couple more functions, maybe we’ll even send it through an XMLHttpRequest call. Now the wrong value is in our server and it ends up in the database. You can see where I’m going with this, right? If we had used the built-in function, which respects the rules, we would immediately spot the bug right then and there. Whenever you write a function, make sure the types it returns are consistent. A bad example is shown below:
function foo(a) {
if(a === 'foo') {
return 'bar';
}

return false;
}
Again, same as with variables and parameters, if we have a function like this, we can’t make assumptions about its behavior. We will need to use an if to check the type of the returned value. We might forget about it at some point, and then we have another bug in our hands. We can rewrite it in many ways, here’s one way which fixes the issue:
function foo(a) {
if(a === 'foo') {
return 'bar';
}

return '';
}
This time we’ve made sure all the paths return a string. It’s much easier to reason about the function’s result now.

null and undefined are Special

So far we’ve really just talked about the primitive types. When it comes to objects and arrays, you should follow the same rules, but there are two special cases to keep in mind. When dealing with reference types, you sometimes need to indicate that there is no value. A good example of this is document.getElementById. If it doesn’t find a matching element, it will return null. This is why we will consider null to share the type with any object or array, but only those. You should avoid returning null from a function which may otherwise return a primitive value like Number. undefined can be also considered a “no value” for references. For most purposes, it can be treated as equal to null, but null is preferred because of its semantics in other object-oriented languages.

Arrays and null

When working with arrays, you should also consider that an empty array is often a better choice than null. Although arrays are reference types and you can use null with them, it usually makes more sense to return an empty array. Let’s look at the following example:
var list = getListOfItems();

for(var i = 0; i < list.length; i++) {
//do something
}
This is probably one of the most common styles of usage for arrays. You get an array from a function, and then you iterate over it to do something else. What would happen in the above code if getListOfItems returned a null when there are no items? It would throw an error, because null does not have length (or any other property for that matter). When you consider the typical usage of arrays like this, or even list.forEach or list.map, you can see how it’s generally a good idea to return an empty array when there are no values.

Type Checking and Type Conversion

Let’s look at type checking and type conversion in more detail. When should you do type checks? When should you do type conversion?

Type Conversion

The first goal with type conversion should be to make sure your values are of the correct type. Numeric values should be Numbers and not Strings and so on. The second goal should be that you only need to convert a value once. The best place to do type conversion is at the source. For example, if you’re fetching data from the server, you should do any necessary type conversion in the function which handles the received data. Parsing data from the DOM is a very common example of where things start to go wrong. Let’s say you have a textbox which contains a number, and you want to read it. Or, it could just be an attribute in some HTML element, it doesn’t even have to be user input.
//This is always going to be a string
var num = numberInput.value;

//This is also always a string
var num2 = myElement.getAttribute('numericAttribute');
Since values that you can get from DOM are often strings, it’s important to do type conversion when reading them. In a way, you can think of it as the “edge” of your module. The data is entering your JavaScript module through this function which is reading it, therefore it has to convert the data into the correct format. By doing type conversion at the edges of our module, we ensure that the internals don’t have to deal with it. This reduces the likelihood of bugs being caused by implicit type coercion by a large margin. It also allows us to write less code because we don’t let bad values to get into the module from the edges.
//We can parse ints and floats like so
var num = parseInt(numberInput.value, 10);
var num2 = parseFloat(myElement.getAttribute('numericAttribute'));

//But if you need to convert a string to a boolean, you need to do a string comparison
var bool = booleanString === 'true';

typeof and Type Checks

You should only use typeof for validation, not branching logic based on type. There are exceptions to this, but it’s a good rule of thumb to follow. Let’s look at two examples for this:
function good(a) {
if(typeof a !== 'number') {
throw new TypeError('a must be a number');
}

//do something
}
This is an example of using typeof for validation. We’re ensuring that the parameter given to the function is of the correct type. However, the following example shows what it means to branch logic by type.
function bad(a) {
if(typeof a === 'number') {
//do something
}
else if(typeof a === 'string') {
//do something
}
else if(typeof a === 'boolean') {
//do something
}
}
Don’t do this. Although it can sometimes be necessary, it’s usually a sign of poor design. If you find yourself doing this kind of logic a lot, you probably should have converted the value earlier in the code into the correct type. If you end up with a lot of typeofs in your code, it can be a sign that you might need to convert the value you’re comparing against. It’s typical for type checks to spread out, and that is often a good sign of poor design with regards to types. As mentioned earlier, you should try to do type conversions at the edges of your module, as it allows you to avoid the typeof cascade. If you do your conversion early on, none of the functions that are called after it have to do typechecks or type conversions. This also applies to objects: If you find yourself doing a lot of checks using instanceof or checking if a property on an object exists, it’s a sign that perhaps you should structure the data differently. The same rule applies to instanceof as typeof: You should try to avoid it, as it can be a sign of poor design. There is one case where it’s unavoidable though:
try {
// some code that throws exceptions
} catch(ex) {
if (ex instanceof TypeError) {

} else if (ex instanceof OtherError) {

}
}
If your code requires specific handling for exception types, instanceof is often a decent choice, since JavaScript catch doesn’t allow to differentiate by type like it does in some other languages. In most other cases, you should try to avoid instanceof.

Conclusion

As we’ve discovered, we get great freedom with JavaScript’s weak typing, but we also must take care to think before we act. Otherwise, we’ll end up in a massive mess of types where nothing makes sense. By making sure our code follows the rule of consistent types, we save ourselves a lot of trouble. It’s much easier to reason about our code when we know the types. We don’t have to build a lot of type checks into our code just to guard against errors. This might seem difficult if you haven’t used languages with strong typing, but it pays back greatly when you need to debug or maintain the code. For further reading on the topic, I would recommend taking a look at TypeScript. It’s a language similar to JavaScript, but it adds stronger typing semantics to the language. It also has a compiler which will spit out errors when you try to do something silly, like mix and match types.

Frequently Asked Questions (FAQs) about Strongly Typed Languages in JavaScript

What is the difference between strongly typed and loosely typed languages?

Strongly typed languages are those where variables are bound to a specific data type, and any operation inconsistent with that type results in an error. Examples include Java and C++. On the other hand, loosely typed languages like JavaScript allow variables to hold any type of data, and type conversion happens automatically when necessary. This flexibility can lead to unexpected results if not handled carefully.

How can I enforce strong typing in JavaScript?

JavaScript is inherently a loosely typed language, but you can enforce a form of strong typing using TypeScript, a statically typed superset of JavaScript. TypeScript adds static types to JavaScript, allowing for type checking at compile time. This can help catch errors early in the development process. Another way is to use the ‘strict mode’ in JavaScript, which makes the language behave more like a strongly typed one by throwing errors for unsafe actions.

What are the benefits of using strongly typed languages?

Strongly typed languages offer several benefits. They can help catch errors at compile time rather than at runtime, which can save a lot of debugging time. They also make code more self-documenting, as the data types of variables give a clear indication of how they should be used. Moreover, they can make code more predictable and easier to reason about, as they prevent unexpected type conversions.

Can I use JavaScript as a strongly typed language?

While JavaScript is not a strongly typed language by default, you can enforce strong typing using tools like TypeScript or Flow. These tools add static types to JavaScript, allowing for type checking at compile time. This can help catch errors early in the development process. However, it’s important to note that these tools do not change the underlying nature of JavaScript; they simply provide a layer of type safety on top of it.

What is TypeScript and how does it relate to JavaScript?

TypeScript is a statically typed superset of JavaScript developed by Microsoft. It adds static types to JavaScript, allowing for type checking at compile time. This can help catch errors early in the development process. TypeScript code is transpiled to JavaScript, meaning it can run anywhere JavaScript runs. It’s fully compatible with JavaScript and can use all of JavaScript’s features.

What are the drawbacks of using strongly typed languages?

While strongly typed languages offer several benefits, they also have some drawbacks. They can be more verbose and require more code to accomplish the same tasks as loosely typed languages. They also require a compilation step, which can slow down the development process. Moreover, they can be less flexible and harder to use for certain tasks, such as working with dynamic data.

How does ‘strict mode’ in JavaScript work?

Strict mode’ is a feature in JavaScript that makes the language behave more like a strongly typed one. It throws errors for unsafe actions, such as assigning to a read-only property or using a variable before it’s declared. This can help catch errors early in the development process. To enable ‘strict mode’, simply add the line ‘use strict’; at the top of your JavaScript file or function.

What is type coercion in JavaScript?

Type coercion is a feature in JavaScript where the language automatically converts one type of data to another when necessary. For example, if you try to add a number and a string, JavaScript will convert the number to a string before performing the addition. While this can be convenient, it can also lead to unexpected results if not handled carefully.

How can I avoid type coercion in JavaScript?

One way to avoid type coercion in JavaScript is to use the ‘strict mode’, which throws errors for unsafe actions. Another way is to use the ‘===’ operator instead of the ‘==’ operator for comparisons, as the former does not perform type coercion. Moreover, you can use tools like TypeScript or Flow to add static types to JavaScript, which can help catch type-related errors at compile time.

What is the future of strongly typed languages in JavaScript?

The use of strongly typed languages in JavaScript is likely to increase in the future, as they offer several benefits such as catching errors early and making code more predictable. Tools like TypeScript and Flow are becoming increasingly popular, and new tools and features are being developed to make JavaScript more type-safe. However, the flexibility and dynamism of JavaScript will continue to make it a popular choice for many developers.

Jani HartikainenJani Hartikainen
View Author

Jani has built all kinds of JS apps for more than 15 years. At his blog, he helps JavaScript developers learn to eliminate bad code so they can focus on writing awesome apps and solve real problems.

code qualityColinIstrongly typed languages
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week